Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble finding library that supports http PUT in Python 2.7

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()
like image 819
Skip Huffman Avatar asked May 07 '26 22:05

Skip Huffman


1 Answers

httplib should manage.

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

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

like image 138
Stephen Paulger Avatar answered May 10 '26 12:05

Stephen Paulger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!