Wednesday, 17 March 2010

Python http PUT request

I have a CherryPy service which accepts 'PUT' requests with a named payload parameter (it's name is , surprisingly, 'payload'). From cURL, this is easy to specify with the -F option, but I couldn't find anywhere how to do this with Python. I discovered something a bit odd, which seems to be that the payload needs to be specified twice:

import urllib, httplib
import time, calendar
import base64

if __name__=='__main__':
timenow=time.gmtime()
t=time.asctime(timenow)
iov1=str(calendar.timegm(timenow))+'000000000'
iov2=str(calendar.timegm(timenow) + 1) +'000000000'
p=''+t+''
px=urllib.quote_plus(p)
pe=urllib.urlencode({"payload":p})
urlbase='putserver.cern.ch:8081'
testPut='DEVDB10/ATLAS_SCT_COMMCOND_DEV/PUTS200/TEST/PUT/Multi/timespan/'+iov1+''+iov2+"?payload="+px
test='/cooldb'
testMerge='/cooldb/merge'
print px
print urlbase
authHeader={"Authorization": "Basic "+base64.encodestring('name:pwd'), "Content-Length":str(len(px))}
#payload=px
h=httplib.HTTPConnection(urlbase)
h.request('PUT',testPut,px,authHeader)
r=h.getresponse()
print r.status, r.reason
print r.read()
h.close()

No comments: