httplib problem

Dirk Engelmann Dirk.Engelmann at IWR.Uni-Heidelberg.De
Mon Dec 13 07:20:36 EST 1999


Hi!

On Fri, 10 Dec 1999, bjorn wrote:

> try
> 
>     print form['data'].value
> 
> as the last line.

The problem is, form['data'] is not initialized (adding .value doesn't
helptherefore).  
Probably the header which is send by the httplib-programm:
 hdr.append('Content-Disposition: form-data; name="data"') 
is not well stated - I don't know how the naming of a field could be done 
in a python script using httplib. In a web-form it works by using 
this field and a submit (button) statement.

Thanks for help!


Cheers,
Dirk


> 
> Dirk Engelmann wrote:
> 
> > 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']
> > # or print form['data'].value
> > --
> > http://www.python.org/mailman/listinfo/python-list
> 





More information about the Python-list mailing list