A Simple Python CGI Server Tutorial - Part 2 - CGI Input/Output
This article will deal with retrieving and using input to your CGI programs. There are two ways to get input from your CGI programs, encoded within the URL and submitted through forms. This tutorial will go over both methods.
We'll need to do a little setup to begin, if you've already read through the first tutorial you should be very familiar with this process.
First we need to create a directory to house our CGI server and scripts, for this tutorial we'll name it 'server'. Create a new file, with the following program, in the 'server' directory and name it 'server.py'.
#!/usr/bin/env python import BaseHTTPServer import CGIHTTPServer server = BaseHTTPServer.HTTPServer handler = CGIHTTPServer.CGIHTTPRequestHandler server_address = ("", 8000) handler.cgi_directories = [""] httpd = server(server_address, handler) httpd.serve_forever()
Give 'server.py' executable permissions ($ chmod +x server.py). The next step is to create a page that will give us a link with an encoded URL. To do this we'll create a simple HTML page with nothing but the encoded link in it. Create a new file named 'link.py' in the 'server' directory, with the following code and give it executable permissions ($ chmod +x link.py).
#!/usr/bin/env python print "Content-type: text/html" print print "<title>Test URL Encoding</title>" print '<p><a href="http://localhost:8000/test_urlencode.py?first=Jack&last=Trades">Link</a></p>'
Now we need to create the CGI script that will receive this data and output it into a new page. Create a new file named 'test_urlencode.py' in the 'server' directory and input the following code.
#!/usr/bin/env python import cgi form = cgi.FieldStorage() val1 = form.getvalue('first') val2 = form.getvalue('last') print "Content-type: text/html" print print "<title>Test URL Encoding</title>" print "<p>" print "Hello my name is " + val1 + " " + val2 print "</p>"
Remember to give executable permissions to all your CGI scripts ($ chmod +x test_urlencode.py). That's it! We now have a working CGI script. To test it type the following into the location bar of your browser.
http://localhost:8000/link.py
Click on the link and the 'test_urlencode.py' script will output a page that says "Hello my name is Jack Trades". You can play around with the output by changing the URL of the link to whatever you want. Below is a sample of URLs and their output, you can type/paste these directly into the location bar (no need to modify the 'link.py' script).
http://localhost:8000/test_urlencode.py?first=Jack&last=Trades
Hello my name is Jack Trades
http://localhost:8000/test_urlencode.py?first=Adam&last=Baum
Hello my name is Adam Baum
http://localhost:8000/test_urlencode.py?first=Anna&last=Grahm
Hello my name is Anna Grahm
Courtesy of Bart Simpson...
http://localhost:8000/test_urlencode.py?last=Tinkle&first=Ivana
Hello my name is Ivana Tinkle
OK, that's enough fun for now. Let's move on to retrieving input from form data. The first thing we must do is create a form to enter some data into. We'll do this by writing another CGI script to output the HTML form. Create another file in the 'server' directory named 'form.py' with the code below.
#!/usr/bin/env python print "Content-type: text/html" print print '<form method="post" action="test_form.py">' print '<textarea name="comments" cols="40" rows="5">' print 'Enter comments here...' print '</textarea>' print '<br>' print '<input type="submit" value="Submit">' print '</form>'
This is the form that will be presented to the user to allow them to enter their 'comments'. Remember to give this script executable permissions ($ chmod +x form.py). Next create another file in the 'server' directory named 'test_form.py', this is the CGI script that will accept the form data and create a new page that includes the data.
#!/usr/bin/env python import cgi form = cgi.FieldStorage() val1 = form.getvalue('comments') print 'Content-type: text/html' print print 'The form input is below...' print '<br>' print val1
Give this file executable permissions as well ($ chmod +x test_form.py) and then go back to your browser and enter the following line in the location bar.
http://localhost:8000/form.py
You should see a form with a single text area and a submit button. Type whatever you want to in the text area, I chose "la de da de, we like to party", and hit the submit button. You should be presented by a page that says...
The form input is below...
la de da de, we like to party
Congratulations you have successfully handled form data with a CGI script. You'll notice that Python's cgi module uses cgi.FieldStorage() to access both URL encoded input as well as form based input. I neglected to go over the scripts line-by-line because there are very few concepts introduced in each of them. If you have any questions, or would like me to clarify something, please leave a comment below.
Moved to Wordpress
This blog has moved to Wordpress. GOTO the new blog now.
There will be no new updates here. Most of the posts have also been expanded.
Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts
2009/05/07
A Simple Python CGI Server Tutorial
A Simple Python CGI Server Tutorial
In this post I'm going to explain how to create a simple Python CGI Server. To illustrate the concepts involved more clearly I will not attempt to make this server extensible, rather I will try to keep the code as simple and clear as possible.
To start with we need to create a directory for our server and CGI scripts to reside in. I simply created a directory named 'server' in my home directory, but you may name it anything you wish and place it anywhere in your filesystem.
The next step is to create a simple CGI server. To do this open up your favorite text editor and write the program below. (I'll go through it line-by-line at the end of this tutorial.)
#!/usr/bin/env python
import BaseHTTPServer
import CGIHTTPServer
import cgitb; cgitb.enable() ## This line enables CGI error reporting
server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
server_address = ("", 8000)
handler.cgi_directories = [""]
httpd = server(server_address, handler)
httpd.serve_forever()
Save this file in the 'server' directory as 'server.py', or whatever name tickles your fancy, and give it executable permissions. On Linux systems this is achieved with the command below...
$ chmod +x server.py
That's it for the server! Simple wasn't that? Now that we have a server ready to go we need something for it to serve. For that we will create a simple "Hello World!" CGI script. Open up your text editor again and type in the following program.
#!/usr/bin/env python
print "Content-type: text/html"
print "<title>Test CGI</title>"
print "<p>Hello World!</p>"
Save this file in the 'server' directory as 'test_cgi.py' and give it executable permissions, just like the server.py file.
$ chmod +x test_cgi.py
Now we have all the components in place. To start your Python CGI server simply open up a terminal and cd into your 'server' directory. When you are there simply type the following command.
$ ./server.py
Your server is now fully operational! To see your first page fire up a browser and type the following into the location bar.
http://localhost:8000/test_cgi.py
You should be greeted by a webpage that proudly proclaims "Hello World!". If you are not, make sure to check your file permissions (both the server and CGI script should be given executable permissions) and make sure you copied the programs above verbatim. To kill your server simply go back to the terminal and hit 'ctrl-c'.
Server Code Line-By-Line
For those of you who want a more in-depth analysis of what this code is doing, here is a line-by-line explanation.
The first thing we need to do is make this script executable on the command line. The following 'she-bang' line simplifies this process so that we can type: ./server.py instead of the more verbose: python server.py
#!/usr/bin/env python
The next two lines import the necessary libraries. The first, BaseHTTPServer, provides us with a simple web server and CGIHTTPServer provides us with a request handler.
import BaseHTTPServer
import CGIHTTPServer
The following line enables in-browser CGI error reporting. This can be very handy as you develop CGI scripts, however if you don't want to expose the internals of your program to users you may wish to log the output of this to a file instead.
import cgitb; cgitb.enable() ## This line enables CGI error reporting
The next two lines create a server and a request handler.
server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
The next line specifies an address for the server, in this case we want to use 'localhost' so simply specifing "" will give us this. This line also specifies a port number to associate with our server. You can choose whatever port number you wish.
server_address = ("", 8000)
The next line specifies where the CGI scripts will reside in relation to the 'server' directory. In this example we put our CGI script in the base directory by using "", however you may perfer to place your scripts in a special 'cgi' or 'cgi-bin' directory.
handler.cgi_directories = [""]
The next line actually 'creates' our server, passing it the server address and port number, as well as the CGI handler. If you're wondering why we name it httpd it stands for HTTP Daemon.
httpd = server(server_address, handler)
The final line puts our program into an infinite loop so that the server can 'serve_forever'. Until you kill the server with 'ctrl-c' from the terminal, it will continue to listen for requests and serve the appropriate web pages.
httpd.serve_forever()
2008/12/17
Pointless Programming Reference
The Pointless Programming Reference is a programming language reference document that expresses programming concepts and their implementation in a number of languages.
The problems are broken into categories, such as Strings, Lists and Functions. Currently there are 4 languages to choose from Chicken Scheme, Python, Javascript and C, and there are over 1,100 solutions.
Check out the Pointless Programming Reference.
I'm also planning to do something like this for widget toolkits (Qt, Gtk, etc) at some point in the future.
The problems are broken into categories, such as Strings, Lists and Functions. Currently there are 4 languages to choose from Chicken Scheme, Python, Javascript and C, and there are over 1,100 solutions.
Check out the Pointless Programming Reference.
I'm also planning to do something like this for widget toolkits (Qt, Gtk, etc) at some point in the future.
Labels:
C,
Javascript,
Python,
Reference,
Scheme
Learn to Program with Python
A short and entirely unfinished introduction to the Python programming language. It covers basic math, variables, functions and some conditionals.
Learn to Program with Python
Learn to Program with Python
Subscribe to:
Posts (Atom)