This is an old copy of the Python FAQ. The information here may be outdated.

How can I mimic CGI form submission (METHOD=POST)?

I would like to retrieve web pages that are the result of POSTing a form. Is there existing code that would let me do this easily?

Yes. Here’s a simple example that uses urllib2.

import urllib
import urllib2

uri = ""
params = dict(activity="inquisition", origin="Spain")

    f = urllib2.urlopen(uri, urllib.urlencode(params))
except urllib2.URLError, e:
    print "Failed:", e
    print "Succeeded:",

The form parameters are added to a dictionary, which is then encoded and passed as the second argument to urllib2.urlopen. The response can then be read as usual.


A Django site. rendered by a django application. hosted by webfaction.