html POST in python

John J. Lee jjl at pobox.com
Mon Jun 28 16:44:10 CEST 2004


"Mark Light" <light at soton.ac.uk> writes:

> Hi I am trying to "post" a file to a webpage and save the output as a
> file. I have looked at
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306
> 
> which seems to have several suggestions - my problem is my lack of
> knowledge of html forms.
> 
> The active part of the webpage form in question is below - how would I
> feed this information into one of the python scripts from the cookbook
> examples. Also would I need to do something about handling the result?
> 
> I have a plain text file in a location say "c:\temp\file.cif"
> 

Try ClientForm (http://wwwsearch.sf.net/ClientForm).  Untested:

import urllib2, StringIO
import ClientForm

url = 'http://blah.com/wherever/your/form/page/lives.html'
r = urllib2.urlopen('http://blah.com/wherever/your/form/page/lives.html')
forms = ClientForm.ParseResponse(r)
r.close()
form = forms[0]

filename = 'c:\temp\file.cif'
f = open(filename)
form.add_file(f, 'application/octet-stream', filename)
r2 = urllib2.urlopen(form.click())

print r2.info()
print "********************************"
print r2.read()
r.close()


John



More information about the Python-list mailing list