python and cgi

Greg Jorgensen gregj at
Sun Dec 24 01:22:23 CET 2000

<etoffi at> wrote in message news:9235p5$ngo$1 at
> hello, fellow pythoners!
> i am writing an application that will communicate between two systems
> using http.  note that i am not an expert in http (or python ;)
> i need a way to "upload" using the HTTP POST method (ie: simulating a
> browser).

One easy way to do this:

1. Get your server-side page working with the form input and submit, so you
can use it from your browser.
For example, let's say the URL of the server-side page is and it has three input fields: firstname,
lastname, and email. To make testing easy you should make the resulting page
(what happens after the form data is submitted) be a single line of text (no
HTML) that has a word or code (like 'ok' or 'fail').

2. Put your form fields into a Python dictionary, then use the urllib module
to encode the form data, open the URL, send the form data, and get the

    import urllib

    url = ''
    formdata = {'firstname': 'greg', 'lastname': 'jorgensen', 'email':
'gregj at'}

        f = urllib.urlopen(url, urllib.urlencode(formdata))
        s =
        s = s.strip().lower()
        s = ''

    if s == 'ok':
        # success
        # fail

This is a very simplistic example but it will get you going. XML-RPC and
SOAP are more robust and scalable solutions.

Greg Jorgensen
Deschooling Society
Portland, Oregon, USA
gregj at

More information about the Python-list mailing list