python and cgi

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


<etoffi at bigfoot.com> wrote in message news:9235p5$ngo$1 at nnrp1.deja.com...
> 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
http://domain.com/formpage.html 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
result:

    import urllib

    url = 'http://domain.com/formpage.html'
    formdata = {'firstname': 'greg', 'lastname': 'jorgensen', 'email':
'gregj at pobox.com'}

    try:
        f = urllib.urlopen(url, urllib.urlencode(formdata))
        s = f.read()
        s = s.strip().lower()
    except:
        s = ''

    if s == 'ok':
        # success
    else:
        # 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 pobox.com





More information about the Python-list mailing list