Python
  Home arrow Python arrow Page 3 - Writing CGI Programs in Python
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
OLM
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Writing CGI Programs in Python
By: Preston Landers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 60
    1999-08-25

    Table of Contents:
  • Writing CGI Programs in Python
  • Why should my next CGI project be in Python?
  • Your First CGI program in Python
  • Getting some real work done
  • Defining a useful Display function
  • Putting the pieces together
  • Simple Database Access
  • Other Resources and Links

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Writing CGI Programs in Python - Your First CGI program in Python
    (Page 3 of 8 )

    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


     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway