在 Python 2.7 中查找支持 http PUT 的库时遇到问题
我需要从 python 执行 http PUT 操作 哪些库已被证明支持此操作?更具体地说,我需要对密钥对执行 PUT,而不是文件上传。
我一直在尝试使用restful_lib.py,但我从正在测试的API 中得到无效结果。 (我知道结果无效,因为我可以从命令行使用curl触发相同的请求,并且它可以工作。)
在参加Pycon 2011之后,我留下的印象是pycurl可能是我的解决方案,所以我一直在尝试实现那。我这里有两个问题。首先,pycurl 将“PUT”重命名为“UPLOAD”,这似乎暗示它专注于文件上传而不是密钥对。其次,当我尝试使用它时,我似乎从未从 .perform() 步骤中获得返回。
这是我当前的代码:
import pycurl
import urllib
url='https://xxxxxx.com/xxx-rest'
UAM=pycurl.Curl()
def on_receive(data):
print data
arglist= [\
('username', '[email protected]'),\
('email', '[email protected]'),\
('username','testUserName'),\
('givenName','testFirstName'),\
('surname','testLastName')]
encodedarg=urllib.urlencode(arglist)
path2= url+"/user/"+"99b47002-56e5-4fe2-9802-9a760c9fb966"
UAM.setopt(pycurl.URL, path2)
UAM.setopt(pycurl.POSTFIELDS, encodedarg)
UAM.setopt(pycurl.SSL_VERIFYPEER, 0)
UAM.setopt(pycurl.UPLOAD, 1) #Set to "PUT"
UAM.setopt(pycurl.CONNECTTIMEOUT, 1)
UAM.setopt(pycurl.TIMEOUT, 2)
UAM.setopt(pycurl.WRITEFUNCTION, on_receive)
print "about to perform"
print UAM.perform()
I need to perform http PUT operations from python Which libraries have been proven to support this? More specifically I need to perform PUT on keypairs, not file upload.
I have been trying to work with the restful_lib.py, but I get invalid results from the API that I am testing. (I know the results are invalid because I can fire off the same request with curl from the command line and it works.)
After attending Pycon 2011 I came away with the impression that pycurl might be my solution, so I have been trying to implement that. I have two issues here. First, pycurl renames "PUT" as "UPLOAD" which seems to imply that it is focused on file uploads rather than key pairs. Second when I try to use it I never seem to get a return from the .perform() step.
Here is my current code:
import pycurl
import urllib
url='https://xxxxxx.com/xxx-rest'
UAM=pycurl.Curl()
def on_receive(data):
print data
arglist= [\
('username', '[email protected]'),\
('email', '[email protected]'),\
('username','testUserName'),\
('givenName','testFirstName'),\
('surname','testLastName')]
encodedarg=urllib.urlencode(arglist)
path2= url+"/user/"+"99b47002-56e5-4fe2-9802-9a760c9fb966"
UAM.setopt(pycurl.URL, path2)
UAM.setopt(pycurl.POSTFIELDS, encodedarg)
UAM.setopt(pycurl.SSL_VERIFYPEER, 0)
UAM.setopt(pycurl.UPLOAD, 1) #Set to "PUT"
UAM.setopt(pycurl.CONNECTTIMEOUT, 1)
UAM.setopt(pycurl.TIMEOUT, 2)
UAM.setopt(pycurl.WRITEFUNCTION, on_receive)
print "about to perform"
print UAM.perform()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
httplib 应该管理。
http://docs.python.org/library/httplib.html
有一个例子此页面http://effbot.org/librarybook/httplib.htm
httplib should manage.
http://docs.python.org/library/httplib.html
There's an example on this page http://effbot.org/librarybook/httplib.htm
urllib 和 urllib2。
urllib and urllib2 are also suggested.
感谢大家的帮助。我想我已经找到答案了。
我的代码现在如下所示:
更新:现在我得到了有效的响应。这似乎是我的解决方案。 (最后的漂亮打印不起作用,但我真的不在乎,它只是在我构建函数时就在那里。)
Thank you all for your assistance. I think I have found an answer.
My code now looks like this:
Updated: Now I am getting valid responses. This seems to be my solution. (The pretty print at the end isn't working, but I don't really care, that is just there while I am building the function.)