有没有办法在python中做HTTP PUT [英] Is there any way to do HTTP PUT in python
问题描述
我需要使用 Python 中的 HTTP PUT
将一些数据上传到服务器.从我对 urllib2 文档的简要阅读来看,它只执行 HTTP POST
.有没有办法在 python 中执行 HTTP PUT
?
I need to upload some data to a server using HTTP PUT
in python. From my brief reading of the urllib2 docs, it only does HTTP POST
. Is there any way to do an HTTP PUT
in python?
推荐答案
我过去使用过各种 Python HTTP 库,我已经选择了 'Requests' 作为我的最爱.现有的库有非常有用的接口,但是对于简单的操作来说,代码最终可能会太长几行.请求中的基本 PUT 如下所示:
I've used a variety of python HTTP libs in the past, and I've settled on 'Requests' as my favourite. Existing libs had pretty useable interfaces, but code can end up being a few lines too long for simple operations. A basic PUT in requests looks like:
payload = {'username': 'bob', 'email': 'bob@bob.com'}
>>> r = requests.put("http://somedomain.org/endpoint", data=payload)
然后您可以检查响应状态代码:
You can then check the response status code with:
r.status_code
或响应:
r.content
Requests 有很多语法糖和快捷方式,可以让您的生活更轻松.
Requests has a lot synactic sugar and shortcuts that'll make your life easier.
这篇关于有没有办法在python中做HTTP PUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!