使用Python urllib2进行PUT

cod*_*rer 9 python urllib2

我正在尝试按照我在stackoverflow上找到的示例使用urllib2进行PUT到REST:

有没有办法在python中进行HTTP PUT

我不明白为什么我得到错误错误.

这是我的代码的摘录:

import urllib2
import json

content_header = {'Content-type':'application/json',
                 'Accept':'application/vnd.error+json,application/json',
                 'Accept-Version':'1.0'}

baseURL = "http://some/put/url/"


f = open("somefile","r")
data = json.loads(f.read())

request = urllib2.Request(url=baseURL, data=json.dumps(jsonObj), headers=content_header)
request.get_method = lambda: 'PUT' #if I remove this line then the POST works fine.

response = urllib2.urlopen(request)

print response.read()
Run Code Online (Sandbox Code Playgroud)

如果我删除PUT选项我试图设置然后它发布它找到但是当我尝试将get_method设置为PUT时它会出错.

为了确保REST服务不会导致我尝试使用cURL执行PUT的问题,它运行正常.

aar*_*fay 7

正如其他人所说,这requests是一个很棒的图书馆.但是,如果您处于requests无法使用的情况(例如ansible模块开发或类似情况),还有另一种方法,正如本要点的作者所证明的那样:

import urllib2

class MethodRequest(urllib2.Request):
    def __init__(self, *args, **kwargs):
        if 'method' in kwargs:
            self._method = kwargs['method']
            del kwargs['method']
        else:
            self._method = None
        return urllib2.Request.__init__(self, *args, **kwargs)

    def get_method(self, *args, **kwargs):
        if self._method is not None:
            return self._method
        return urllib2.Request.get_method(self, *args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

用法:

>>> req = MethodRequest(url, method='PUT')
Run Code Online (Sandbox Code Playgroud)


Hoo*_*ons 7

尽管aaronfay的答案很好并且可行,但我认为,鉴于GET之外只有3种HTTP方法(并且您只担心PUT),为每个方法定义Request子类更加清晰和简单。

例如:

class PutRequest(urllib2.Request):
    '''class to handling putting with urllib2'''

    def __init__(self, *args, **kwargs):
        return urllib2.Request.__init__(self, *args, **kwargs)

    def get_method(self, *args, **kwargs):
        return 'PUT'
Run Code Online (Sandbox Code Playgroud)

然后使用:

request = PutRequest(url, data=json.dumps(data), headers=content_header)
Run Code Online (Sandbox Code Playgroud)