html POST in python

M

Mark Light

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"

""""

<form method="post" enctype="multipart/form-data"
action="http://scripts.iucr.org/cgi-bin/checkcif.pl">File name:<br />
<input type="file" name="file" size="35">
<input type="submit" name="UPLOAD" value="Send"> <br />
<br />

</form>


""""


Any advei would be very useful to me.

Mark.
 
J

John J. Lee

Mark Light said:
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
 
S

simo

# create array of name/value pairs
self.params = urllib.urlencode({'user': 'fred', 'password': 'hax0r5'})

# send http-post
request = urllib.urlopen("http://www.domain.com/login.cgi", params)

# read back each line of reply
line = request.readline()
 
J

John J. Lee

# create array of name/value pairs
self.params = urllib.urlencode({'user': 'fred', 'password': 'hax0r5'})

# send http-post
request = urllib.urlopen("http://www.domain.com/login.cgi", params)

# read back each line of reply
line = request.readline()

That's a generic POST, but there's a bit more to it for file upload,
which is what the OP seemed to want.

If you don't want the HTML parsing that my rather heavy ClientForm
library does (heavyweight for the job it does, anyway), there are
functions lying around for doing file upload in traditional procedural
style. Can't remember where, but no doubt they're Google-able for.


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top