Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

The

element in line 9 specifies the location of the Python script that you want to process the
form data. The form contains two text boxes, a series of radio buttons to specify one age range as a
single return value, a series of check boxes to select one or more hobbies, and a text area for entering
comments.


The web form is just HTML code, and you need to place it in the standard /var/www folder to
serve it to web clients:


Click here to view code image


sudo cp script2207.html /var/www
sudo chmod +x /var/www/script2207.html

You can then open your browser to view the form by using this URL:


Click here to view code image


http://localhost/script2207.html

You should see a form like the one in Figure 22.7.


FIGURE 22.7 The basic web form used for the Python script.

Now you’re ready to write the Python script to retrieve and process the form data.


The cgi Module


When the webpage passes the form data back to the Apache web server, it groups the data in
key/value pairs. The key is the HTML element name for the form field in the webpage, and the value
is the data value that was entered in the form field.


For example, this HTML code associates the key name lname with the value entered into that form
field:

Free download pdf