如何在 Python 中发出 PATCH 请求?

发布于 2024-11-26 20:12:37 字数 98 浏览 1 评论 0原文

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

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

Is there a way to make a request using the PATCH HTTP method in Python?

I tried using httplib, but it doesn't accept PATCH as method param.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

七分※倦醒 2024-12-03 20:12:37

使用 请求,使 PATCH 请求 非常简单:

import requests

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

With Requests, making PATCH requests is very simple:

import requests

r = requests.patch('http://httpbin.org/patch')
始终不够 2024-12-03 20:12:37

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

Seems to work in 2.7.1 as well.

>>> 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
一萌ing 2024-12-03 20:12:37

我在 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 方法

I tried this in Python 3, and it seemed to work (but I don't have a server handy that supports the PATCH request type):

>>> 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

I'm assuming that the HTTP 405 is coming from the server and that it is "not allowed".

By the way, thanks for showing me the cool PATCH method in HTTP.

夜吻♂芭芘 2024-12-03 20:12:37

httplib2 非常简单:

import httplib2

http = httplib2.Http()
http.request("http://www.google.com", "PATCH", <patch content>)

它支持 Python 2.3 或更高版本(包括 3.x)而且效果很好!

It is incredibly simple with httplib2:

import httplib2

http = httplib2.Http()
http.request("http://www.google.com", "PATCH", <patch content>)

It supports Python 2.3 or later (including 3.x) and works beautifully!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文