Creating Self-Posting CGI Scripts


#!/usr/bin/pythonimport cgi, os, sys data = cgi.FieldStorage() formText = """Content-type: text/html\n <form method="POST" action="cgi_selfpost.cgi">     Name <input type="TEXT" name="name">     Quote <input type="TEXT" name="quote" size="80">     <input type="SUBMIT" value="send"> </FORM> """ print formText if data.has_key('name') and data.has_key('quote'):     f = open("quotes.dat", 'a')     f.write("<li><b>%s:</b> %s</li>\n" % \             (data['name'].value, data['quote'].value))     f=open("quotes.dat", 'r') if f:     print f.read()

A self-posting CGI script is one that posts to itself. Self-posting scripts enable you to keep all your code in a single file rather than spread it out through multiple HTML and CGI files.

In addition to the first line, you will need to add code to parse the data from the CGI posts, handle the parameters from the CGI post, and write forms to the web browser that posts the CGI script.

Note

In the example, the self-posting form is added to the script even if no parameters are passed when the CGI script is loaded. However, the initial posting to the script can be from another script or web page, as well as a self-post from the same script.


Typically, you will want to parse the data and handle arguments first because most self-posting CGI scripts will write different views back to the web browser depending on what parameters were posted.

The CGI post data can be parsed using the cgi.FieldStorage() function. FieldStorage returns a dictionary of fields that were included with the request.

Parameters can be accessed from the dictionary returned by FieldStorage by using the standard Python syntax to access the keys and values of the dictionary. In the example, has_key(key) is used to determine whether a key exists, and then the value is directly accessed using the d[key].value syntax.

After you have accessed the parameters, you can use their values to determine what HTML view needs to be sent back to the web browser through stdout, which writes back to the web browser.

Note

Each time a post is received, the CGI script is reloaded. No local or global data is retained. If you need to have data survive between multiple posts, you will need to store it locally on the server. In the following example, the quotes are captured and stored in a local data file on the server so that they can be displayed each time a new post is received.


import cgi, os, sys #Send errors to browser sys.stderr = sys.stdout #Parse data from form data = cgi.FieldStorage() #Send form to browser formText = """Content-type: text/html\n <title>CGI Self-Post Form</title>\n <h2>Enter Quote</h2><P> <form method="POST" action="cgi_selfpost.cgi">     Name <input type="TEXT" name="name">     <p>     Quote <input type="TEXT" name="quote" size="80">     <p>     <input type="SUBMIT" value="send"> </form> <hr> <h2>Received Quotes</h2><p>""" print formText #Retrieve field from form and store data if data.has_key('name') and data.has_key('quote'):     f = open("quotes.dat", 'a')     f.write("<li><b>%s:</b> %s</li>\n" % \             (data['name'].value, data['quote'].value))     f.close() #Send stored data to browser f=open("quotes.dat", 'r') if f:     print f.read()     f.close()


cgi_selfpost.cgi

<LI><B>King Arthur:</B> I am your king!</LI> <LI><B>Peasant:</B> I didn't vote for you.</LI> <LI><B>King Arthur: </B> You don't vote for a king!</LI> <LI><B>Black Knight:</B> None shall pass!</LI> <LI><B>Bridge Keeper: </B> What is the air speed velocity of  an unlaiden swallow?</LI>


Contents of quotes.dat data file.

Figure 10.4 displays the web page that cgi_selfpost.cgi generates as items are posted to it.

Figure 10.4. Web browser view of cgi_selfpost.cgi.




Python Phrasebook(c) Essential Code and Commands
Python Phrasebook
ISBN: 0672329107
EAN: 2147483647
Year: N/A
Pages: 138
Authors: Brad Dayley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net