3

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', '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()
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Skip Huffman
  • 5,239
  • 11
  • 41
  • 51

3 Answers3

3

httplib should manage.

http://docs.python.org/library/httplib.html

There's an example on this page http://effbot.org/librarybook/httplib.htm

Stephen Paulger
  • 5,204
  • 3
  • 28
  • 46
2

urllib and urllib2 are also suggested.

Drake Guan
  • 14,514
  • 15
  • 67
  • 94
0

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

Skip Huffman
  • 5,239
  • 11
  • 41
  • 51