Writing a guestbook for your (Python-based) web server with CGI

Common Gateway Interface (CGI) is a standard in web programming by which custom scripts can be used to produce web server output. You would like to catch the HTML form input from a user's browser, redirect it to another page, and acknowledge a user action.

How to do it...

We first need to run a web server that supports CGI scripts. We placed our Python CGI script inside a cgi-bin/ subdirectory and then visited the HTML page that contains the feedback form. Upon submitting this form, our web server will send the form data to the CGI script, and we'll see the output produced by this script.

Listing 5.7 shows us how the Python web server supports CGI:

#!/usr/bin/env python
# Python Network Programming Cookbook -- Chapter - 5
# This program is optimized for Python 2.7.
# It may run on any other version with/without modifications.

import os
import cgi
import argparse
import BaseHTTPServer
import CGIHTTPServer
import cgitb 
cgitb.enable()  ## enable CGI error reporting
def web_server(port):
    server = BaseHTTPServer.HTTPServer
    handler = CGIHTTPServer.CGIHTTPRequestHandler #RequestsHandler
    server_address = ("", port)
    handler.cgi_directories = ["/cgi-bin", ]
    httpd = server(server_address, handler)
    print "Starting web server with CGI support on port: %s ..." %port
    httpd.serve_forever()
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='CGI Server Example')
    parser.add_argument('--port', action="store", dest="port", type=int, required=True)
    given_args = parser.parse_args()
    web_server(given_args.port)

The following screenshot shows CGI enabled web server is serving contents:

How to do it...

If you run this recipe, you will see the following output:

$ python 5_7_cgi_server.py --port=8800
Starting web server with CGI support on port: 8800 ...
localhost - - [19/May/2013 18:40:22] "GET / HTTP/1.1" 200 -

Now, you need to visit http://localhost:8800/5_7_send_feedback.html from your browser.

You will see an input form. We assume that you provide the following input to this form:

Name:  User1
Comment: Comment1

The following screenshot shows the entering user comment in a web form:

How to do it...

Then, your browser will be redirected to http://localhost:8800/cgi-bin/5_7_get_feedback.py where you can see the following output:

User1 sends a comment: Comment1

The user comment is shown in the browser:

How to do it...

How it works...

We have used a basic HTTP server setup that can handle CGI requests. Python provides these interfaces in the BaseHTTPServer and CGIHTTPserver modules.

The handler is configured to use the /cgi-bin path to launch the CGI scripts. No other path can be used to run the CGI scripts.

The HTML feedback form located on 5_7_send_feedback.html shows a very basic HTML form containing the following code:

<html>
   <body>
         <form action="/cgi-bin/5_7_get_feedback.py" method="post">
                Name: <input type="text" name="Name">  <br />
                Comment: <input type="text" name="Comment" />
                <input type="submit" value="Submit" />
         </form>
   </body>
</html>

Note that the form method is POST and action is set to the /cgi-bin/5_7_get_feedback.py file. The contents of this file are as follows:

#!/usr/bin/env python
# Python Network Programming Cookbook -- Chapter - 5
# This program requires Python 2.7 or any later version
import cgi
import cgitb 
# Create instance of FieldStorage 
form = cgi.FieldStorage() 
# Get data from fields
name = form.getvalue('Name')
comment  = form.getvalue('Comment')
print "Content-type:text/html

"
print "<html>"
print "<head>"
print "<title>CGI Program Example </title>"
print "</head>"
print "<body>"
print "<h2> %s sends a comment: %s</h2>" % (name, comment)
print "</body>"
print "</html>"

In this CGI script, the FieldStorage() method is called from cgilib. This returns a form object to process the HTML form inputs. Two inputs are parsed here (name and comment) using the getvalue() method. Finally, the script acknowledges the user input by echoing a line back saying that the user x has sent a comment.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.4.179