using python to post to a HTML form

Romuald Texier rtexier at elikya.com
Thu Apr 5 05:12:08 EDT 2001


dsavitsk wrote:


> from the python docs ...
> 
> >>> import httplib, urllib
> >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
> >>> h = httplib.HTTP("www.musi-cal.com:80")
> >>> h.putrequest("POST", "/cgi-bin/query")
> >>> h.putheader("Content-length", "%d" % len(params))
> >>> h.putheader('Accept', 'text/plain')
> >>> h.putheader('Host', 'www.musi-cal.com')
> >>> h.endheaders()
> >>> h.send(params)
> >>> reply, msg, hdrs = h.getreply()
> >>> print reply # should be 200
> >>> data = h.getfile().read() # get the raw HTML
> 

Even simpler (POST method):
        params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
        data = urllib.urlopen("http://www.musi-cal.com:80", params) # returns a 
file-like object containing the HTML response file


-- 
Romuald Texier



More information about the Python-list mailing list