0% found this document useful (0 votes)
202 views

CGI Programming: Python3 Advanced #7

CGI (Common Gateway Interface) allows Python scripts to interface with web servers to dynamically generate webpages in response to user input. This tutorial explains how to set up an Apache web server and write basic Python CGI scripts that can retrieve form data submitted by users and print output. It provides an example of a simple "hello" script that grabs a submitted name and prints "hello" customized with that name. The cgi module handles GET and POST requests and allows accessing submitted form fields.

Uploaded by

radanpetrica
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
202 views

CGI Programming: Python3 Advanced #7

CGI (Common Gateway Interface) allows Python scripts to interface with web servers to dynamically generate webpages in response to user input. This tutorial explains how to set up an Apache web server and write basic Python CGI scripts that can retrieve form data submitted by users and print output. It provides an example of a simple "hello" script that grabs a submitted name and prints "hello" customized with that name. The cgi module handles GET and POST requests and allows accessing submitted form fields.

Uploaded by

radanpetrica
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

CGI Programming

Python3 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 server’s cgi-bin
directory.
What is CGI Programming?
• CGI programming is writing dynamically
generating WebPages that respond to user input
or WebPages that interact with software on the
server.
• Things like Dropbox use python for it’s web
interface.
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 don’t 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 distro’s) 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.
Basic CGI python script
• Lets create a basic script that just prints out that
it worked and print hello world 5 times.
• Lets call it hello.py
• Must have a line telling the web server where
python is installed eg.
#!/usr/bin/python
• We also have to change the file permissions so it
can execute. chmod 755 hello.py
The cgi module
• The cgi module contains a lot of declarations
and does a lot of initializing in the background.
• Because of this Never use:
from cgi import …
• Provides us with methods to get POST and GET
requests from the web server.
• As well as a few other methods for parsing data.
Using Form data
• The python cgi module handles POST and GET with the same
method.
• cgi.FieldStorage() this method will return a FieldStorage
object that allows us to get data from a submitted form.
• We can user the FieldStorage object like a python dictionary
or we can use the getvalue() method. (I suggest not using
this method as it will crash if there is more than one field
with the same name. but for now it’s fine)
• Alternative methods are getfirst() and getlist() (Safe)
Simple hello ‘insert name’ program
• Let’s create a simple script that grabs the
entered name from POST and prints out hello
to that name.
• Let’s just modify our hello.py file.

• Then lets add a check box to see if they are


happy or sad or both,
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.

You might also like