Cgi Programing
Cgi Programing
Python Advanced #7
Preamble
This tutorial is about web interfacing,
Having a basic understanding of HTML
and how it works will be very helpful.
If you have done any PHP before this
will be familiar however in python.
What is CGI?
CGI stands for Common Gateway Interface.
CGI is the standard for programs to
interface with HTTP servers. (Some other
servers too)
This means we can take information from
things like Forms.
CGI scripts generally go in a web servers
cgi-bin directory.
Setup
To be able to get our CGI to work we
need a web server we can play with.
If you have your own web server and
want to use that go ahead.
If you dont lets quickly set up and
apache2 web server on our Ubuntu
box.
Setup
To install Apache2 (which should be a
standard package in almost all linux distros)
we use:
sudo apt-get install apache2
Then we must set the web server permissions
to allow cgi programs to execute.
For simplicity we will allow cgi to execute in
the root directory.
Notes
When debugging you code you can use the cgitb
module to output errors to the page.
Import cgitb
cgitb.enable()
The cgitb.enable() can be modified so that the error
messages are saved to a file rather than output to
users of your script. This can be done with:
cgitb.enable(display=0, logdir=/path/)
cgi.escape() Try to always escape user input if you
plan to use it!
Next
Database Interaction
Feel free to leave any questions in
the comments.