httplib problem
Dirk Engelmann
dirk.engelmann at iwr.uni-heidelberg.de
Fri Dec 10 19:08:29 EST 1999
Hi!
I want to transfer data by http betweeen a http-server (cgi-script) and
a client (httplib).
I tried this using httplib on the client side and transfering data
to a cgi-script, but it failed.
I appended a litele sample - which didn´t work and I can´t find
a solution.
Is there another easier way to do this ?
Thanks for any help!
Cheers,
Dirk Engelmann
#------------- http-client -------------------
import httplib
from string import joinfields
# sample data which should be transfered to the cgi-script port.py
data = "testdata"
# header:
hdr = []
part = []
hdr.append('Content-Disposition: form-data; name="data"')
hdr.append('Content-Type: application/octet-stream')
hdr.append('Content-Length: %d' % len(data))
part.append("%s\n\n%s" % (joinfields(hdr,'\n'), data))
# st contains header and data
st = joinfields(part, '')
# open httplib and execute cgi-script port.py
h = httplib.HTTP('localhost')
boundary= '%s%s_%s_%s' % \
('-----', int(time()), os.getpid(), randint(1,10000))
contentType = 'multipart/form-data; boundary=%s' % boundary
h.putrequest('POST', '/cgi-bin/port.py')
h.putheader('Accept', '*/*')
h.putheader('Content-Type', contentType)
h.putheader('Content-Length', str(len(st)))
# transfer the data to cgi-script port.py
h.endheaders()
h.send(st)
errcode, errmsg, headers = h.getreply()
data = h.getfile().read() # Get the raw HTML
print data
#--------------- port.py : cgi-script on server -----------------
import cgi
form = cgi.FieldStorage()
print form['data']
More information about the Python-list
mailing list