Home arrow Python arrow Page 3 - Writing CGI Programs in Python

Your First CGI program in Python - Python

And now for something completely different... Python has a very extensive, well documented and portable module library that provides a large variety of useful functions. The Internet-related collection is particularly impressive, with modules to deal with everything from parsing and retrieving URL's to retrieving mail from POP servers and everything in between.

TABLE OF CONTENTS:
  1. Writing CGI Programs in Python
  2. Why should my next CGI project be in Python?
  3. Your First CGI program in Python
  4. Getting some real work done
  5. Defining a useful Display function
  6. Putting the pieces together
  7. Simple Database Access
  8. Other Resources and Links
By: Preston Landers
Rating: starstarstarstarstar / 78
August 25, 1999

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
In this section, I'm going to assume you have Python installed and have set up your web server to actually execute Python scripts (instead of, for example, sending the source code to the client.) There are so many variations of operating systems and web servers that I'm going to have to rely on you following the appropriate directions that came with your copy of Python.

A tip for Unix users: often a CGI script has to have the executable attribute set ("chmod +x script.py") and/or have a special extension or be in a special CGI directory. Consult your friendly local web administrator for details.

Please note that if you're using Apache, using mod_python is not absolutely necessary but is recommended for most applications. It doesn't affect anything in your program other than the (perceived) execution speed because it eliminates the start-up time for the interpreter with each connection.

Here is a (very) simple script to test your setup:


#!/usr/bin/python # Tell the browser how to render the text print "Content-Type: text/plain\n\n" print "Hello, Python!" # print a test string

Let's go over the script line-by-line to familiarize ourselves with the basic components of a Python script.

The first line is necessary for most Linux/Unix systems, but is harmlessly useless on all other systems. It is simply a signal to the shell that This Is A Python Program. You will need to change the path if Python is not installed in the usual place. (Sometimes it is in /usr/local/bin/python for example.) Other systems, such as Windows or Macintosh, will have to follow different procedures to inform the OS to execute this as a Python file. Since the first line begins with a # character, it is a comment and has no actual effect in the Python program itself.

Next we have another comment describing what the first line of actual code does: simply print a string (to "standard output") describing what kind of content follows. This is necessary for most browsers to know how to display the information. In this case it's just plain text, but we might want 'Content-Type: text/html' in the future. The '\n' is a special character that says "print a new line." Normally the print command will automatically add a newline for you, but in this case we need two (because that's what browsers expect.) The signals a special character. Use to actually print a slash.

Finally, we simply say hello to the world! Note that Python statements do not end with a semicolon or other punctuation, just a newline.

If everything went according to plan, visiting the URL of the above script should print "Hello, Python!" to your browser window.



 
 
>>> More Python Articles          >>> More By Preston Landers
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: