有没有办法在python中进行HTTP PUT [英] Is there any way to do HTTP PUT in python

查看:247
本文介绍了有没有办法在python中进行HTTP PUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在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库,我已经确定了'请求'作为我的最爱。现有的库具有相当可用的接口,但是对于简单的操作,代码最终可能会变得太长。请求中的基本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 has a lot synactic sugar and shortcuts that'll make your life easier.

这篇关于有没有办法在python中进行HTTP PUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆