在 Python 2.7 中查找支持 http PUT 的库时遇到问题

发布于 2024-10-22 23:47:19 字数 1340 浏览 1 评论 0原文

我需要从 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 技术交流群。

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

发布评论

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

评论(3

迷爱 2024-10-29 23:47:19

urllib and urllib2 are also suggested.

甜点 2024-10-29 23:47:19

感谢大家的帮助。我想我已经找到答案了。

我的代码现在如下所示:

import urllib
import httplib
import lxml
from lxml import etree
url='xxxx.com'
UAM=httplib.HTTPSConnection(url)

arglist= [\
    ('username', '[email protected]'),\
    ('email', '[email protected]'),\
    ('username','testUserName'),\
    ('givenName','testFirstName'),\
    ('surname','testLastName')\
    ]
encodedarg=urllib.urlencode(arglist)

uuid="99b47002-56e5-4fe2-9802-9a760c9fb966"
path= "/uam-rest/user/"+uuid
UAM.putrequest("PUT", path)
UAM.putheader('content-type','application/x-www-form-urlencoded')
UAM.putheader('accepts','application/com.internap.ca.uam.ama-v1+xml')
UAM.putheader("Content-Length", str(len(encodedarg)))
UAM.endheaders()
UAM.send(encodedarg)
response = UAM.getresponse()
html = etree.HTML(response.read())
result = etree.tostring(html, pretty_print=True, method="html")
print result

更新:现在我得到了有效的响应。这似乎是我的解决方案。 (最后的漂亮打印不起作用,但我真的不在乎,它只是在我构建函数时就在那里。)

Thank you all for your assistance. I think I have found an answer.

My code now looks like this:

import urllib
import httplib
import lxml
from lxml import etree
url='xxxx.com'
UAM=httplib.HTTPSConnection(url)

arglist= [\
    ('username', '[email protected]'),\
    ('email', '[email protected]'),\
    ('username','testUserName'),\
    ('givenName','testFirstName'),\
    ('surname','testLastName')\
    ]
encodedarg=urllib.urlencode(arglist)

uuid="99b47002-56e5-4fe2-9802-9a760c9fb966"
path= "/uam-rest/user/"+uuid
UAM.putrequest("PUT", path)
UAM.putheader('content-type','application/x-www-form-urlencoded')
UAM.putheader('accepts','application/com.internap.ca.uam.ama-v1+xml')
UAM.putheader("Content-Length", str(len(encodedarg)))
UAM.endheaders()
UAM.send(encodedarg)
response = UAM.getresponse()
html = etree.HTML(response.read())
result = etree.tostring(html, pretty_print=True, method="html")
print result

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

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