0% found this document useful (0 votes)
7 views21 pages

Python Notes Unit 4

The document provides an overview of the Common Gateway Interface (CGI) in Python, detailing how it facilitates communication between web servers and custom scripts. It covers the architecture of CGI, web server configuration, and examples of CGI scripts for handling GET and POST methods, as well as form inputs like checkboxes, radio buttons, and text areas. Additionally, it discusses the use of cookies for maintaining session information in web applications.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views21 pages

Python Notes Unit 4

The document provides an overview of the Common Gateway Interface (CGI) in Python, detailing how it facilitates communication between web servers and custom scripts. It covers the architecture of CGI, web server configuration, and examples of CGI scripts for handling GET and POST methods, as well as form inputs like checkboxes, radio buttons, and text areas. Additionally, it discusses the use of cookies for maintaining session information in web applications.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit 4

Python CGI

• The Common Gateway Interface, or CGI, is a set of standards that define how information is
exchanged between the web server and a custom script. The CGI specs are currently maintained
by the NCSA (National Center for Supercomputing Applications).

CGI

• The Common Gateway Interface, or CGI, is a standard for external gateway programs to interface
with information servers such as HTTP servers.

• The current version is CGI/1.1 and CGI/1.2 is under progress.

Web Browsing

• Your browser contacts the HTTP web server and demands for the URL, i.e., filename.

• Web Server parses the URL and looks for the filename. If it finds that file then sends it back to the
browser, otherwise sends an error message indicating that you requested a wrong file.

• Web browser takes response from web server and displays either the received file or error
message.

• However, it is possible to set up the HTTP server so that whenever a file in a certain directory is
requested that file is not sent back; instead it is executed as a program, and whatever that program
outputs is sent back for your browser to display. This function is called the Common Gateway
Interface or CGI and the programs are called CGI scripts. These CGI programs can be a Python
Script, PERL Script, Shell Script, C or C++ program, etc.

CGI Architecture Diagram

Web Server Support and Configuration


• Before you proceed with CGI Programming, make sure that your Web Server supports CGI and it is
configured to handle CGI Programs. All the CGI Programs to be executed by the HTTP server are
kept in a pre-configured directory. This directory is called CGI Directory and by convention it is
named as /var/www/cgi-bin. By convention, CGI files have extension as. cgi, but you can keep your
files with python extension .py as well.

• By default, the Linux server is configured to run only the scripts in the cgi-bin directory in /var/www.
If you want to specify any other directory to run your CGI scripts, comment the following lines in the
[Link] file –

<Directory "/var/www/cgi-bin">
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>

<Directory "/var/www/cgi-bin">
Options All
</Directory>

• Here, we assume that you have Web Server up and running successfully and you are able to run
any other CGI program like Perl or Shell, etc.

First CGI Program

There is a simple link, which is linked to a CGI script called [Link]. This file is kept in /var/www/cgi-bin
directory and it has following content. Before running your CGI program, make sure you have change
mode of file using chmod 755 [Link] UNIX command to make file executable.

#!/usr/bin/python

print "Content-type:text/html\r\n\r\n"

print '<html>'

print '<head>'

print '<title>Hello World - First CGI Program</title>'

print '</head>'

print '<body>'
print '<h2>Hello World! This is my first CGI program</h2>'

print '</body>'

print '</html>'

If you click [Link], then this produces the following output −

Hello World! This is my first CGI program

HTTP Header

• The line Content-type:text/html\r\n\r\n is part of HTTP header which is sent to the browser to
understand the content. All the HTTP header will be in the following form −

HTTP Field Name: Field Content


Example

Content-type: text/html\r\n\r\n

There are few other important HTTP headers, which you will use frequently in your CGI Programming.

[Link]. Header & Description

1
Content-type:

A MIME string defining the format of the file being returned. Example is Content-type:text/html

2
Expires: Date

The date the information becomes invalid. It is used by the browser to decide when a page
needs to be refreshed. A valid date string is in the format 01 Jan 1998 12:00:00 GMT.

3
Location: URL

The URL that is returned instead of the URL requested. You can use this field to redirect a
request to any file.

4
Last-modified: Date

The date of last modification of the resource.

5
Content-length: N
The length, in bytes, of the data being returned. The browser uses this value to report the
estimated download time for a file.

6
Set-Cookie: String

Set the cookie passed through the string

CGI Environment Variables

• All the CGI programs have access to the following environment variables. These variables play an
important role while writing any CGI program.

[Link]. Variable Name & Description

1
CONTENT_TYPE

The data type of the content. Used when the client is sending attached content to the server.
For example, file upload.

2
CONTENT_LENGTH

The length of the query information. It is available only for POST requests.

3
HTTP_COOKIE

Returns the set cookies in the form of key & value pair.

4
HTTP_USER_AGENT

The User-Agent request-header field contains information about the user agent originating
the request. It is name of the web browser.

5
PATH_INFO

The path for the CGI script.

6
QUERY_STRING

The URL-encoded information that is sent with GET method request.


7
REMOTE_ADDR

The IP address of the remote host making the request. This is useful logging or for
authentication.

8
REMOTE_HOST

The fully qualified name of the host making the request. If this information is not available,
then REMOTE_ADDR can be used to get IR address.

9
REQUEST_METHOD

The method used to make the request. The most common methods are GET and POST.

10
SCRIPT_FILENAME

The full path to the CGI script.

11
SCRIPT_NAME

The name of the CGI script.

12
SERVER_NAME

The server's hostname or IP Address

13
SERVER_SOFTWARE

The name and version of the software the server is running.

Here is small CGI program to list out all the CGI variables. Click this link to see the result Get
Environment

#!/usr/bin/python

import os

print "Content-type: text/html\r\n\r\n";

print "<font size=+1>Environment</font><\br>";


for param in [Link]():

print "<b>%20s</b>: %s<\br>" % (param, [Link][param])

GET and POST Methods

• You must have come across many situations when you need to pass some information from your
browser to web server and ultimately to your CGI Program. Most frequently, browser uses two
methods two pass this information to web server. These methods are GET Method and POST
Method.

Passing Information using GET method

• The GET method sends the encoded user information appended to the page request.
• You can pass information by simply concatenating key and value pairs along with any URL or you
can use HTML <FORM> tags to pass information using GET method.

Simple URL Example:Get Method

• Here is a simple URL, which passes two values to hello_get.py program using GET method.

/cgi-bin/hello_get.py?first_name=ZARA&last_name=ALI

• Below is hello_get.py script to handle input given by web browser. We are going to
use cgi module, which makes it very easy to access passed information –

#!/usr/bin/python

# Import modules for CGI handling

import cgi, cgitb

# Create instance of FieldStorage

form = [Link]()

# Get data from fields

first_name = [Link]('first_name')

last_name = [Link]('last_name')

print "Content-type:text/html\r\n\r\n"

print "<html>"

print "<head>"

print "<title>Hello - Second CGI Program</title>"

print "</head>"
print "<body>"

print "<h2>Hello %s %s</h2>" % (first_name, last_name)

print "</body>"

print "</html>"

This would generate the following result −

Hello ZARA ALI

Simple FORM Example: GET Method

• This example passes two values using HTML FORM and submit button. We use same CGI script
hello_get.py to handle this input.

<form action = "/cgi-bin/hello_get.py" method = "get">

First Name: <input type = "text" name = "first_name"> <br />

Last Name: <input type = "text" name = "last_name" />

<input type = "submit" value = "Submit" />

</form>

Here is the actual output of the above form, you enter First and Last Name and then click submit
button to see the result.

First Name:

Submit
Last Name:

Passing Information Using POST Method

• A generally more reliable method of passing information to a CGI program is the POST method.
This packages the information in exactly the same way as GET methods, but instead of sending it
as a text string after a? in the URL it sends it as a separate message. This message comes into the
CGI script in the form of the standard input.
• Below is same hello_get.py script which handles GET as well as POST method.

#!/usr/bin/python

# Import modules for CGI handling

import cgi, cgitb


# Create instance of FieldStorage

form = [Link]()

# Get data from fields

first_name = [Link]('first_name')

last_name = [Link]('last_name')

print "Content-type:text/html\r\n\r\n"

print "<html>"

print "<head>"

print "<title>Hello - Second CGI Program</title>"

print "</head>"

print "<body>"

print "<h2>Hello %s %s</h2>" % (first_name, last_name)

print "</body>"

print "</html>"

To take again same example as above which passes two values using HTML FORM and submit
button. We use same CGI script hello_get.py to handle this input.

<form action = "/cgi-bin/hello_get.py" method = "post">

First Name: <input type = "text" name = "first_name"><br />

Last Name: <input type = "text" name = "last_name" />

<input type = "submit" value = "Submit" />

</form>

There is the actual output of the above form. You enter First and Last Name and then click submit button to
see the result.

First Name:

Submit
Last Name:

Passing Checkbox Data to CGI Program

• Checkboxes are used when more than one option is required to be selected.
• Here is example HTML code for a form with two checkboxes −

<form action = "/cgi-bin/[Link]" method = "POST" target = "_blank">


<input type = "checkbox" name = "maths" value = "on" /> Maths

<input type = "checkbox" name = "physics" value = "on" /> Physics

<input type = "submit" value = "Select Subject" />

</form>

The result of this code is the following form −

Select Subject
Maths Physics

Below is [Link] script to handle input given by web browser for checkbox button.

#!/usr/bin/python

# Import modules for CGI handling

import cgi, cgitb

# Create instance of FieldStorage

form = [Link]()

# Get data from fields

if [Link]('maths'):

math_flag = "ON"

else:

math_flag = "OFF"

if [Link]('physics'):

physics_flag = "ON"

else:

physics_flag = "OFF"

print "Content-type:text/html\r\n\r\n"
print "<html>"

print "<head>"

print "<title>Checkbox - Third CGI Program</title>"

print "</head>"

print "<body>"

print "<h2> CheckBox Maths is : %s</h2>" % math_flag

print "<h2> CheckBox Physics is : %s</h2>" % physics_flag

print "</body>"

print "</html>"

Passing Radio Button Data to CGI Program

• Radio Buttons are used when only one option is required to be selected.
• Here is example HTML code for a form with two radio buttons −

<form action = "/cgi-bin/[Link]" method = "post" target = "_blank">

<input type = "radio" name = "subject" value = "maths" /> Maths

<input type = "radio" name = "subject" value = "physics" /> Physics

<input type = "submit" value = "Select Subject" />

</form>

The result of this code is the following form −

Select Subject
Maths Physics

Below is [Link] script to handle input given by web browser for radio button −

#!/usr/bin/python

# Import modules for CGI handling

import cgi, cgitb

# Create instance of FieldStorage


form = [Link]()

# Get data from fields

if [Link]('subject'):

subject = [Link]('subject')

else:

subject = "Not set"

print "Content-type:text/html\r\n\r\n"

print "<html>"

print "<head>"

print "<title>Radio - Fourth CGI Program</title>"

print "</head>"

print "<body>"

print "<h2> Selected Subject is %s</h2>" % subject

print "</body>"

print "</html>"

Passing Text Area Data to CGI Program

• TEXTAREA element is used when multiline text has to be passed to the CGI Program.
• Here is example HTML code for a form with a TEXTAREA box −

<form action = "/cgi-bin/[Link]" method = "post" target = "_blank">

<textarea name = "textcontent" cols = "40" rows = "4">

Type your text here...

</textarea>

<input type = "submit" value = "Submit" />

</form>

The result of this code is the following form −


Submit

Below is [Link] script to handle input given by web browser −

#!/usr/bin/python

# Import modules for CGI handling

import cgi, cgitb

# Create instance of FieldStorage

form = cgi. ()FieldStorage

# Get data from fields

if [Link]('textcontent'):

text_content = [Link]('textcontent')

else:

text_content = "Not entered"

print "Content-type:text/html\r\n\r\n"

print "<html>"

print "<head>";

print "<title>Text Area - Fifth CGI Program</title>"

print "</head>"

print "<body>"

print "<h2> Entered Text Content is %s</h2>" % text_content

print "</body>"

Passing Drop Down Box Data to CGI Program


• Drop Down Box is used when we have many options available but only one or two will be selected.
• Below is [Link] script to handle input given by web browser.

#!/usr/bin/python

# Import modules for CGI handling

import cgi, cgitb

# Create instance of FieldStorage

form = [Link]()

# Get data from fields

if [Link]('dropdown'):

subject = [Link]('dropdown')

else:

subject = "Not entered"

print "Content-type:text/html\r\n\r\n"

print "<html>"

print "<head>"

print "<title>Dropdown Box - Sixth CGI Program</title>"

print "</head>"

print "<body>"

print "<h2> Selected Subject is %s</h2>" % subject

print "</body>"

print "</html>"

Using Cookies in CGI


• HTTP protocol is a stateless protocol. For a commercial website, it is required to maintain session
information among different pages. For example, one user registration ends after completing many
pages. How to maintain user's session information across all the web pages?
• In many situations, using cookies is the most efficient method of remembering and tracking
preferences, purchases, commissions, and other information required for better visitor experience
or site statistics.

How It Works?

• Your server sends some data to the visitor's browser in the form of a cookie. The browser may
accept the cookie. If it does, it is stored as a plain text record on the visitor's hard drive. Now, when
the visitor arrives at another page on your site, the cookie is available for retrieval. Once retrieved,
your server knows/remembers what was stored.
• Cookies are a plain text data record of 5 variable-length fields −
o Expires − The date the cookie will expire. If this is blank, the cookie will expire when the
visitor quits the browser.
o Domain − The domain name of your site.
o Path − The path to the directory or web page that sets the cookie. This may be blank if you
want to retrieve the cookie from any directory or page.
o Secure − If this field contains the word "secure", then the cookie may only be retrieved with
a secure server. If this field is blank, no such restriction exists.
o Name=Value − Cookies are set and retrieved in the form of key and value pairs.

Setting up Cookies

It is very easy to send cookies to browser. These cookies are sent along with HTTP Header before to
Content-type field. Assuming you want to set UserID and Password as cookies. Setting the cookies is done
as follows −

#!/usr/bin/python

print "Set-Cookie:UserID = XYZ;\r\n"

print "Set-Cookie:Password = XYZ123;\r\n"

print "Set-Cookie:Expires = Tuesday, 31-Dec-2007 23:12:40 GMT";\r\n"

print "Set-Cookie:Domain = [Link];\r\n"

print "Set-Cookie:Path = /perl;\n"

print "Content-type:text/html\r\n\r\n"

...........Rest of the HTML Content....


• From this example, you must have understood how to set cookies. We use Set-Cookie HTTP
header to set cookies.
• It is optional to set cookies attributes like Expires, Domain, and Path. It is notable that cookies are
set before sending magic line "Content-type:text/html\r\n\r\n.

Retrieving Cookies

• It is very easy to retrieve all the set cookies. Cookies are stored in CGI environment variable
HTTP_COOKIE and they will have following form −

key1 = value1;key2 = value2;key3 = value3....

Example of how to retrieve cookies.

#!/usr/bin/python

# Import modules for CGI handling

from os import environ

import cgi, cgitb

if environ.has_key('HTTP_COOKIE'):

for cookie in map(strip, split(environ['HTTP_COOKIE'], ';')):

(key, value ) = split(cookie, '=');

if key == "UserID":

user_id = value

if key == "Password":

password = value

print "User ID = %s" % user_id

print "Password = %s" % password

This produces the following result for the cookies set by above script −

User ID = XYZ
Password = XYZ123

File Upload Example

• To upload a file, the HTML form must have the enctype attribute set to multipart/form-data. The
input tag with the file type creates a "Browse" button.

<html>

<body>
<form enctype = "multipart/form-data"

action = "save_file.py" method = "post">

<p>File: <input type = "file" name = "filename" /></p>

<p><input type = "submit" value = "Upload" /></p>

</form>

</body>

</html>

The result of this code is the following form −

File:

Upload

How To Raise a "File Download" Dialog Box?

• Sometimes, it is desired that you want to give option where a user can click a link and it will pop up
a "File Download" dialogue box to the user instead of displaying actual content. This is very easy and can
be achieved through HTTP header. For example, if you want make a FileName file downloadable from a
given link, then its syntax is as follows −

#!/usr/bin/python

# HTTP Header

print "Content-Type:application/octet-stream; name = \"FileName\"\r\n";

print "Content-Disposition: attachment; filename = \"FileName\"\r\n\n";

# Actual File Content will go here.

fo = open("[Link]", "rb")

str = [Link]();

print str

# Close opend file

[Link]()

Network Programming in Python


• Python provides two levels of access to network services. At a low level, you can access the basic
socket support in the underlying operating system, which allows you to implement clients and
servers for both connection-oriented and connectionless protocols.
• Python also has libraries that provide higher-level access to specific application-level network
protocols, such as FTP, HTTP, and so on.

Sockets

• Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate
within a process, between processes on the same machine, or between processes on different
continents.
• Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP,
UDP, and so on. The socket library provides specific classes for handling the common transports
as well as a generic interface for handling the rest.
• Sockets have their own vocabulary −

[Link]. Term & Description

1
Domain

The family of protocols that is used as the transport mechanism. These values are constants
such as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on.

2
type

The type of communications between the two endpoints, typically SOCK_STREAM for
connection-oriented protocols and SOCK_DGRAM for connectionless protocols.

3
protocol

Typically zero, this may be used to identify a variant of a protocol within a domain and type.

4
hostname

The identifier of a network interface −

• A string, which can be a host name, a dotted-quad address, or an IPV6 address in colon
(and possibly dot) notation

• A string "<broadcast>", which specifies an INADDR_BROADCAST address.

• A zero-length string, which specifies INADDR_ANY, or


• An Integer, interpreted as a binary address in host byte order.

5
port

Each server listens for clients calling on one or more ports. A port may be a Fixnum port number,
a string containing a port number, or the name of a service.

The socket Module

• To create a socket, you must use the [Link]() function available in socket module, which
has the general syntax −

s = [Link] (socket_family, socket_type, protocol=0)

• Here is the description of the parameters −


o socket_family − This is either AF_UNIX or AF_INET, as explained earlier.
o socket_type − This is either SOCK_STREAM or SOCK_DGRAM.
o protocol − This is usually left out, defaulting to 0.
• Once you have socket object, then you can use required functions to create your client or server
program. Following is the list of functions required −

Server Socket Methods

[Link]. Method & Description

1
[Link]()

This method binds address (hostname, port number pair) to socket.

2
[Link]()

This method sets up and start TCP listener.

3
[Link]()

This passively accept TCP client connection, waiting until connection arrives (blocking).

Client Socket Methods


[Link]. Method & Description

1
[Link]()

This method actively initiates TCP server connection.

General Socket Methods

[Link]. Method & Description

1
[Link]()

This method receives TCP message

2
[Link]()

This method transmits TCP message

3
[Link]()

This method receives UDP message

4
[Link]()

This method transmits UDP message

5
[Link]()

This method closes socket

6
[Link]()

Returns the hostname.

A Simple Server

• To write Internet servers, we use the socket function available in socket module to create a socket
object. A socket object is then used to call other functions to setup a socket server.
• Now call bind(hostname, port) function to specify a port for your service on the given host.
• Next, call the accept method of the returned object. This method waits until a client connects to the
port you specified, and then returns a connection object that represents the connection to that
client.

#!/usr/bin/python # This is [Link] file

import socket # Import socket module

s = [Link]() # Create a socket object

host = [Link]() # Get local machine name

port = 12345 # Reserve a port for your service.

[Link]((host, port)) # Bind to the port

[Link](5) # Now wait for client connection.

while True:

c, addr = [Link]() # Establish connection with client.

print 'Got connection from', addr

[Link]('Thank you for connecting')

[Link]() # Close the connection

A Simple Client

• Let us write a very simple client program which opens a connection to a given port 12345 and given
host. This is very simple to create a socket client using Python's socket module function.
• The [Link](hosname, port ) opens a TCP connection to hostname on the port. Once you
have a socket open, you can read from it like any IO object. When done, remember to close it, as
you would close a file.
• The following code is a very simple client that connects to a given host and port, reads any
available data from the socket, and then exits −

#!/usr/bin/python # This is [Link] file

import socket # Import socket module

s = [Link]() # Create a socket object

host = [Link]() # Get local machine name


port = 12345 # Reserve a port for your service.

[Link]((host, port))

print [Link](1024)

[Link]() # Close the socket when done

Now run this [Link] in background and then run above [Link] to see the result.

# Following would start a server in background.

$ python [Link] &

# Once server is started run client as follows:

$ python [Link] would produce following result −

Got connection from ('[Link]', 48437)


Thank you for connecting

You might also like