Python interacting with Java Script webpage?

Gerhard Häring gerhard.nospam at bigfoot.de
Wed Mar 14 08:57:09 EST 2001


On Tue, 13 Mar 2001 19:37:14 GMT, William Dandreta wrote:
>I would like to write a Python program that will be able to connect to a
>webpage that runs a Java Script, post data to the form displayed, and save
>the data retrieved to a file.

Hmm. As I see it, the JavaScript is irrelevant here. What you probably want is
to have a CGI script output a HTML page with a form included. And the form
fields should be already filled with default values or values that were stored
earlier, like user preferences. Am I right?

I have attached a quick and dirty CGI script that should show the basics of how
this can be done.

>I have been looking for a book, tutorial, or any docs that will take me from
>not having a clue as to how to do it to being able to write the program.

For an example, you can look at the source code of the CGI module, there is a
test function in there that uses most of the modules functionality and is also
useful for debugging.

Gerhard

#####################################################################

#!/usr/bin/env python

# FillForm demo script

import string, cgi

def printRetrievedData():
    # could write data to file instead
    print cgi.FieldStorage(), '<br>' * 3

def printForm( fields, submiturl ):
    print '<form action="%s">' % ( submiturl )
    for ( key, value ) in fields.items():
            print '%s: <input type="text" name="%s" value="%s"><br>'\
              % ( key, key, value )
    print '<input type="submit"><br>'
    print '</form>'

# Mapping of name of form fields to values
form_fields = { 'Name' : 'Gerhard', 'favlanguage' : 'Python' }

# URL of the CGI-Script, probably to be adjusted
cgi_url = '/cgi-bin/ff.py'

print 'Content-type: text/html\n'

print '<html><body><p>'
printRetrievedData()
printForm( form_fields, cgi_url )
print '</p></body></html>'

-- 
mail:   gerhard <at> bigfoot <dot> de
web:    http://highqualdev.com



More information about the Python-list mailing list