Troubleshooting library that supports HTTP PUT in Python 2.7

I need to perform http PUT operations from python. What libraries have been proven to support this? In particular, I need to execute PUT on keyboards, and not upload files.

I try to work with restful_lib.py, but I get incorrect results from the API that I am testing. (I know that the results are invalid because I can run the same query using curl from the command line and it works.)

After visiting Pycon 2011, I got the impression that pycurl might be my solution, so I tried to implement this. I have two questions. First, pycurl renames "PUT" as "UPLOAD", which apparently implies that it focuses on downloading files, rather than key pairs. Secondly, when I try to use it, I never 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', 'testEmailAdd@test.com'),\
    ('email', 'testEmailAdd@test.com'),\
    ('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()
+3
source share
3 answers
+3
source

urllib and urllib2 are also suggested.

+2
source

. , .

:

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

arglist= [\
    ('username', 'testEmailAdd@test.com'),\
    ('email', 'testEmailAdd@test.com'),\
    ('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 get valid answers. This seems to be my decision. (Fine printing at the end doesn't work, but I don't care, it's just as long as I build the function.)

0
source

All Articles