[BioPython] entering text into web forms

Catherine Letondal letondal at pasteur.fr
Fri Jun 20 16:37:49 EDT 2003


"Omid Khalouei" wrote:
> Hello,
> 
> I'm a relatively new biopython user. I know how to open the websites from 
> within python scripts, but I was wondering if it's also possible to enter a 
> text into a form window. I mean is it possible to for example specify a 
> parameter for an alignment inside our python script to be placed in a web 
> form window? If yes could you please give me a hint of how to do that?
> 
> Thanks alot for your help.
> 
> Sam K.
> 

Hi,

You can use httplib. 

There are good books explaining this as well (see:
"Web programming in Python", which is a very good book - not only for web 
programming (http://www.unixreview.com/documents/s=7460/uni1031085776057/ur0209c.htm).

Example :

Say that you want to use Entrez Web form and search a term 
provided as argument on the command line in the nucleotide db.

You have several solutions:
	1) do a GET
	2) do a POST + application/x-www-form-urlencoded content-type
	3) do a POST + multipart/form-data

   #3 would be worth is you submitted, say, a big sequence to a sequence 
	analysis server - which is not the case here 


1) do a GET

from httplib import HTTP
from sys import argv

req = HTTP('www.ncbi.nlm.nih.gov')
req.putrequest("GET",
               '/entrez/query.fcgi?db=nucleotide&cmd=search&term=' + argv[1])
req.putheader("Accept", "text/html")
req.putheader("User-Agent", argv[0])
req.endheaders()

ec, em, h = req.getreply()
print ec, em
f=req.getfile()
l=f.read()
f.close()

print l


2) do a POST + application/x-www-form-urlencoded content-type

from httplib import HTTP
from sys import argv

params = 'db=nucleotide&cmd=search&term=' + argv[1]
content_length = "%d" % len(params)

req = HTTP('www.ncbi.nlm.nih.gov')
req.putrequest("POST", '/entrez/query.fcgi')

req.putheader("Accept", "text/html")
req.putheader("User-Agent", argv[0])
req.putheader("Content-Length", content_length)
req.putheader("Content-type", "application/x-www-form-urlencoded")
req.endheaders()

req.send(params)

ec, em, h = req.getreply()
print ec, em
f=req.getfile()
l=f.read()
f.close()
print l

3) do a POST + multipart/form-data

what is tricky here is to build the body of the request with proper format, e.g you
need boundaries etc... and my solution can probably be enhanced.


from httplib import HTTP

from sys import argv

boundary ="3350843711048987223263301088"
postdata = "--" + boundary

postdata += "\r\nContent-Disposition: form-data; name=\"db\""
postdata += "\r\n\r\n" + 'nucleotide' + "\r\n"
postdata += "--" + boundary
postdata += "\r\nContent-Disposition: form-data; name=\"cmd\""
postdata += "\r\n\r\n" + 'search' + "\r\n"
postdata += "--" + boundary
postdata += "\r\nContent-Disposition: form-data; name=\"term\""
postdata += "\r\n\r\n" + argv[1] + "\r\n"
postdata += "--" + boundary
postdata += "--\r\n"

content_length = str(len(postdata))

req = HTTP('www.ncbi.nlm.nih.gov')
req.putrequest("POST", '/entrez/query.fcgi')

req.putheader("Accept", "text/html")
req.putheader("User-Agent", argv[0])
req.putheader("Content-Length", content_length)
req.putheader("Content-type", "multipart/form-data; boundary=" + boundary)
req.endheaders()

req.send(postdata)

ec, em, h = req.getreply()
print ec, em
f=req.getfile()
l=f.read()
f.close()
print l



--
Catherine Letondal -- Pasteur Institute Computing Center


More information about the BioPython mailing list