2010-07-04 59 views

回答

1

当你提供的数据参数POST将要使用的方法是隐含设置在urlopen通话

urllib.request.urlopen(url, data=None[, timeout]) 

我不认为这是可以使用DELETE HTTP方法与urlib,因为这行:

Request.get_method()
返回一个字符串 指示HTTP请求方法。 这只对HTTP 请求有意义,并且当前总是返回 'GET'或'POST'。

考虑使用httplibhttplib2,或者Twisted代替。对于更好地支持的HTTP方法。

7

requests库可以处理POST,PUT,DELETE和所有其他HTTP方法,并且明显比urllib,httplib及其变体更可怕。

4

您可以覆盖GET_METHOD像这样的东西:

def _make_request(url, data, method): 
    request.urllib2.Request(url, data=data) 
    request.get_method = lambda: method 

然后你通过 “删除” 的方法。

This答案涵盖了详细信息。

0

urllib中库的默认HTTP方法是POST和GET:

def get_method(self): 
    """Return a string indicating the HTTP request method.""" 
    default_method = "POST" if self.data is not None else "GET" 
    return getattr(self, 'method', default_method) 

但我们可以覆盖这个GET_METHOD()函数来获得DELETE请求:

req = urllib.request.Request(new_url) 
req.get_method = lambda: "DELETE"