如何在Python中发出修补程序请求?

2024-05-15 17:14:22 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法在Python中使用PATCHHTTP方法发出请求?

我试过使用httplib,但它不接受PATCH作为方法参数。


Tags: 方法参数httplibpatch发出请求办法patchhttp
3条回答

似乎也适用于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

我在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 method

使用Requests,制作PATCH requests非常简单:

import requests

r = requests.patch('http://httpbin.org/patch')

相关问题 更多 >