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 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.
Your server is now fully operational! To see your first page fire up a browser and type the following into the location bar.
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
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.