如何在Python中发送PATCH请求?
有没有办法在Python中使用PATCH
这个HTTP方法来发送请求呢?
我试过用httplib这个库,但它不支持把PATCH作为方法参数。
4 个回答
4
我在Python 3中试过这个,感觉可以用(不过我手头没有支持PATCH
请求类型的服务器):
>>> import http.client
>>> c = http.client.HTTPConnection("www.google.com")
>>> r = c.request("PATCH", "/index.html")
>>> print(r.status, r.reason)
405 Method Not Allowed
我猜HTTP 405
是服务器发来的,意思是“这个请求不被允许”。
顺便说一下,感谢你让我了解了HTTP中的酷炫的PATCH方法。
16
看起来在2.7.1版本也能正常工作。
>>> import urllib2
>>> request = urllib2.Request('http://google.com')
>>> request.get_method = lambda: 'PATCH'
>>> resp = urllib2.urlopen(request)
Traceback (most recent call last):
...
urllib2.HTTPError: HTTP Error 405: Method Not Allowed