fetching a POST webpage...

reed reedobrien at gmail.com
Thu Jul 6 22:52:55 EDT 2006


bruce wrote:
> hi...
>
> i have the basic code to fetcha url/web page. however, i'm trying to fetch a
> page that uses a FORM/POST. has anyone done this, i've tried a few times
> without success.
>
> i've analyzed the data stream using Firefox/Livehttpheaders to get the HTTP
> stream.. but i'm doing something wrong, and can't quite see what the
> err/issue is...
>
> if you've done this kind of thing, and you have some thoughts, let me know.
> i can send you the output of the livehttpheaders app, and the test code that
> i have...
>
> thanks..
>
> -bruce

If you read the page and parse the form or already know the values you
need to POST this may help.

>From http://www.python.org/doc/current/lib/httplib-examples.html

Here is an example session that shows how to "POST" requests:

>>> import httplib, urllib
>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> headers = {"Content-type": "application/x-www-form-urlencoded",
...            "Accept": "text/plain"}
>>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
>>> conn.request("POST", "/cgi-bin/query", params, headers)
>>> response = conn.getresponse()
>>> print response.status, response.reason
200 OK
>>> data = response.read()
>>> conn.close()




More information about the Python-list mailing list