Unit IV V Python
Unit IV V Python
Output:
Hello world
GeeksforGeeks
123 456
Example 2: In this example, we will extract a string that contains all characters in the
Python file then we can use [Link]().
Output:
Hello world
GeeksforGeeks
123 456
Example 3: In this example, we will see how we can read a file using the with statement in
Python.
Output:
Hello world
GeeksforGeeks
123 456
Example 4: Another way to read a file is to call a certain number of characters like in the
following code the interpreter will read the first five characters of stored data and return it
as a string:
# Python code to illustrate read() mode character wise
file = open("[Link]", "r")
print ([Link](5))
Output:
Hello
Example 5: We can also split lines while reading files in Python. The split() function splits
the variable when space is encountered. You can also split using any characters as you
wish.
Output:
['Hello', 'world']
['GeeksforGeeks']
['123', '456']
Creating a File using the write() Function
Just like reading a file in Python, there are a number of ways to Writing to file in Python .
Let us see how we can write the content of a file using the write() function in Python.
Working in Write Mode
Example 1: In this example, we will see how the write mode and the write() function is
used to write in a file. The close() command terminates all the resources in use and frees the
system of this particular program.
Output:
This is the write commandIt allows us to write in a particular file
Example 2: We can also use the written statement along with the with() function.
Output:
Hello World!!!
Working of Append Mode
Let us see how the append mode works.
Example: For this example, we will use the Python file created in the previous example.
# Python code to illustrate append() mode
file = open('[Link]', 'a')
[Link]("This will add this line")
[Link]()
Output:
This is the write commandIt allows us to write in a particular fileThis will add this line
There are also various other commands in Python file handling that are used to handle
various tasks:
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
It is designed to provide much cleaner syntax and exception handling when you are
working with code. That explains why it’s good practice to use them with a statement
where applicable. This is helpful because using this method any files opened will be closed
automatically after one is done, so auto-cleanup.
Implementing all the functions in File Handling
In this example, we will cover all the concepts that we have seen above. Other than those,
we will also see how we can delete a file using the remove() function from Python os
module.
import os
def create_file(filename):
try:
with open(filename, 'w') as f:
[Link]('Hello, world!\n')
print("File " + filename + " created successfully.")
except IOError:
print("Error: could not create file " + filename)
def read_file(filename):
try:
with open(filename, 'r') as f:
contents = [Link]()
print(contents)
except IOError:
print("Error: could not read file " + filename)
def append_file(filename, text):
try:
with open(filename, 'a') as f:
[Link](text)
print("Text appended to file " + filename + " successfully.")
except IOError:
print("Error: could not append to file " + filename)
def rename_file(filename, new_filename):
try:
[Link](filename, new_filename)
print("File " + filename + " renamed to " + new_filename + " successfully.")
except IOError:
print("Error: could not rename file " + filename)
def delete_file(filename):
try:
[Link](filename)
print("File " + filename + " deleted successfully.")
except IOError:
print("Error: could not delete file " + filename)
if __name__ == '__main__':
filename = "[Link]"
new_filename = "new_example.txt"
create_file(filename)
read_file(filename)
append_file(filename, "This is some additional text.\n")
read_file(filename)
rename_file(filename, new_filename)
read_file(new_filename)
delete_file(new_filename)
Output:
File [Link] created successfully.
Hello, world!
Text appended to file [Link] successfully.
Hello, world!
This is some additional text.
File [Link] renamed to new_example.txt successfully.
Hello, world!
This is some additional text.
File new_example.txt deleted successfully.
DIRECTORIES
Directories are a way of storing, organizing, and separating the files on a computer. The
directory that does not have a parent is called a root directory. The way to reach the file is
called the path. The path contains a combination of directory names, folder names
separated by slashes and colon and this gives the route to a file in the system.
Directory management using Python
Python contains several modules that has a number of built-in functions to manipulate and
process data. Python has also provided modules that help us to interact with the operating
system and the files. These kinds of modules can be used for directory management also.
The modules that provide the functionalities are listed below:
os and [Link]
filecmp
tempfile
shutil
The os module is used to handle files and directories in various ways. It provides provisions
to create/rename/delete directories. This allows even to know the current working directory
and change it to another. It also allows one to copy files from one directory to another. The
major methods used for directory management is explained below.
Creating new directory:
[Link](name) method to create a new directory.
The desired name for the new directory is passed as the parameter.
By default it creates the new directory in the current working directory.
If the new directory has to be created somewhere else then that path has to be specified
and the path should contain forward slashes instead of backward ones.
import os
# creates in current working directory
[Link]('new_dir')
# creates in D:\
[Link]('D:/new_dir')
import os
print("String format :", [Link]())
print("Byte string format :", [Link]())
Output:
String format : /home/nikhil/Desktop/gfg
Byte string format : b'/home/nikhil/Desktop/gfg'
Renaming a directory:
[Link]() method is used to rename the directory.
The parameters passed are old_name followed by new_name.
If a directory already exists with the new_name passed, OSError will be raised in case
of both Unix and Windows.
If a file already exists with the new_name, in Unix no error arises, the directory will be
renamed. But in Windows the renaming won’t happen and error will be raised.
[Link](‘old_name’,’dest_dir:/new_name’) method works similar
to [Link]() but it moves the renamed file to the specified destination
directory(dest_dir).
For example, consider there is a file named ‘[Link]’ in current working directory. Now to
just rename it :
import os
[Link]('[Link]','file1_renamed.txt')
If renaming and moving the file to some other directory is required, then the code snippet
should be:
import os
[Link]('[Link]', 'D:/file1_renamed.txt')
import os
print("Current directory :", [Link]())
# Changing directory
[Link]('/home/nikhil/Desktop/')
print("Current directory :", [Link]())
Output:
Current directory : /home/nikhil/Desktop/gfg
Current directory : /home/nikhil/Desktop
Listing the files in a directory
A directory may contain sub-directories and a number of files in it. To list
them, [Link]() method is used.
It either takes no parameter or one parameter.
If no parameter is passed, then the files and sub-directories of the CWD is listed.
If files of any other directory other than the CWD is required to be listed, then that
directory’s name/path is passed as parameter.
For example: Listing the files in the CWD- GeeksforGeeks (root directory)
import os
print("The files in CWD are :",[Link]([Link]()))
Output:
The files in CWD are : [‘site folder’, ‘.directory’, ‘[Link]’, ‘poem [Link]’, ‘left bar’,
‘images’, ‘Welcome to GeeksforGeeks!\nPosts Add [Link]’, ‘[Link]’, ‘[Link]’,
‘Sorry, you can not update core for some [Link]’, ‘gfgNikhil [Link]’, ‘[Link]’,
‘gfg’, ‘[Link]’, ‘raju’, ‘images big’]
Removing a directory
[Link]() method is used to remove/delete a directory.
The parameter passed is the path to that directory.
It deletes the directory if and only if it is empty, otherwise raises an OSError.
For example, Let us consider a directory K:/files. Now to remove it, one has to ensure
whether it is empty and then proceed for deleting.
import os
dir_li=[Link]('k:/files')
if len(dir_li)==0:
print("Error!! Directory not empty!!")
else:
[Link]('k:/files')
import os
# current working directory of
# GeeksforGeeks
cwd='/'
print([Link](cwd))
# Some other directory
other='K:/'
print([Link](other))
Output
True
False
To get size of the directory:
[Link](path_name) gives the size of the directory in bytes.
OSError is raised if, invalid path is passed as parameter.
import os
print([Link]([Link]()))
Output
4096
Getting access and modification times:
To get the last accessed time of a directory : [Link] (path)
To get the last modified time of the directory : [Link] (path)
These methods return the number of seconds since the epoch. To format it, datetime
module’s strftime( ) function can be used.
Example : Getting access and modification time of GeeksforGeeks (root) directory
import os
import datetime as dt
print("Before conversion :")
# returns seconds since epoch
print("last access time :",[Link]([Link]()))
print("last modification time :",[Link]([Link]()))
print("After conversion :")
# formatting the return value
access_time=[Link]([Link]([Link]())).strftime('%Y-%m-%d %I:%M
%p')
modification_time=[Link]([Link]([Link]())).strftime('%Y-%m-%d %I:
%M %p')
print("last access time :",access_time)
print("last modification time :",modification_time)
Output
Before conversion :
last access time : 1596897099.56
last modification time : 1596897099.56
After conversion :
last access time : 2020-08-08 02:31 PM
last modification time : 2020-08-08 02:31 PM
filecmp module
This module provides various functions to perform comparison between files and
directories. To compare the directories, an object has to be created for the
class [Link] that describes which files to ignore and which files to hide from the
functions of this class. The constructor has to be invoked before calling any of the functions
of this class. The constructor can be invoked as given below:
d = filecmp . dircmp( dir_1, dir_2, ignore=[a,b,c]/None, hide=[d,e,f]/None )
Functions for comparing directories:
1. [Link]() : Compares the two directories given while invoking the constructor and
presents a summary regarding the list of files in both the directories. In case any
identical files are found, they are also listed. Common sub-directories are also printed in
the output.
import filecmp as fc
import os
dir_1 = dir_2 = [Link]()
Output
comparison 1 :
diff / /
Common subdirectories : ['bin', 'boot', 'dev', 'etc', 'home', 'lib', 'lib64', 'media', 'mnt', 'opt',
'proc', 'run', 'sbin', 'srv', 'sys', 'tmp', 'usr', 'var']
2. d.report_partial_closure() : Prints out the comparison between the directories
passed and also the comparisons between the immediate common sub-directories.
import filecmp as fc
import os
dir_1 = dir_2 = [Link]()
# creating object and invoking constructor
d = [Link](dir_1, dir_2, ignore=None, hide=None)
print("comparison 2 :")
d.report_partial_closure()
import filecmp as fc
import os
dir_1 = dir_2 = [Link]()
# creating object and invoking constructor
d = [Link](dir_1, dir_2, ignore=None, hide=None)
print("comparison 3 :")
d.report_full_closure()
tempfile module:
import tempfile as tf
f = [Link](suffix='', prefix='tmp')
print(f)
Output
/tmp/tmp0ndvk7p_
TemporaryDirectory():
This function makes use of mkdtemp() to create a temporary directory and the same
parameters are passed here also.
The main difference is, the object created as a result acts as a context manager.
The user need not manually delete the created temporary file, it is cleared automatically
by the file system.
To get the name of the newly created temp directory, [Link] can be used.
To delete the temporary dir explicitly, cleanup( ) function can be used.
import tempfile as tf
f = [Link](suffix='', prefix='tmp')
print("Temporary file :", [Link])
[Link]()
Output
Temporary file : /tmp/tmpp3wr65fj
shutil module
This module is concerned with number of high-level operations on files and directories. It
allows copying/moving directories from a source to destination.
[Link](s, d, symlinks=False, ignore=None, copy_function=copy2,
ignore_dangling_symlinks=False):
Recursively copies the source directory (s) to the destination directory (d) provided that
‘d’ does not already exists.
symlinks are also known as symbolic links which denote some virtual files or reference
folders or folders that located somewhere else. It can take values like true, false or
omitted. If true, the symlinks in source are marked as symlinks in the destination also,
but the associated metadata will not be copied. If the value is false or omitted, the
contents as well as the metadata of the linked file are copied to the destination.
For ignore, a callable that takes the directory being visited and its contents like the
return value of [Link]() since, copytree() is a recursive functions. The files to be
ignored while copying can be named using this parameter.
The copy2 function is used as the default copy_function because it allows copying
metadata. copy(), copystat() can also be used.
[Link](path, ignore_errors=False, onerror=None):
Deletes an entire directory. This overcomes the main disadvantage of [Link]() that it
deletes the directory only if it is empty.
The path should be a valid directory. The symlinks to a directory won’t be accepted.
If ignore_error is true, then the errors raised while removing the directory will be
ignored. If false or omitted is given, then the raised errors should be handled by the
ones mentioned in onerror parameter.
onerror is a callable that takes three parameters namely function, path and excinfo. The
first is the function that raises the exception, the next is the path to be passed to the
function and the last is the exception information returned by sys.exc_info().
[Link](s,d):
Recursively moves the source directory to the destination specified.
The destination is not supposed to exist already, if exists based on the [Link]()
semantics, it will be overwritten.
CSV (Comma Separated Values) format is the most common import and export format for
spreadsheets and databases. It is one of the most common methods for exchanging data
between applications and popular data format used in Data Science. It is supported by a wide
range of applications. A CSV file stores tabular data in which each data field is separated by
a delimiter(comma in most cases). To represent a CSV file, it must be saved with the .csv file
extension.
Reading from CSV file
Python contains a module called csv for the handling of CSV files. The reader class from the
module is used for reading data from a CSV file. At first, the CSV file is opened using the
open() method in ‘r’ mode(specifies read mode while opening a file) which returns the file
object then it is read by using the reader() method of CSV module that returns the reader
object that iterates throughout the lines in the specified CSV document. Syntax:
[Link](csvfile, dialect='excel', **fmtparams
Note: The ‘with‘ keyword is used along with the open() method as it simplifies exception
handling and automatically closes the CSV file. Example: Consider the below CSV file –
import csv
# opening the CSV file
with open('[Link]', mode ='r')as file:
# reading the CSV file
csvFile = [Link](file)
# displaying the contents of the CSV file
for lines in csvFile:
print(lines)
Output:
[['Steve', 13, 'A'],
['John', 14, 'F'],
['Nancy', 14, 'C'],
['Ravi', 13, 'B']]
Writing to CSV file
[Link] class is used to insert data to the CSV file. This class returns a writer object which
is responsible for converting the user’s data into a delimited string. A CSV file object should
be opened with newline=” otherwise, newline characters inside the quoted fields will not be
interpreted correctly. Syntax:
[Link](csvfile, dialect='excel', **fmtparams)
[Link] class provides two methods for writing to CSV. They
are writerow() and writerows().
writerow(): This method writes a single row at a time. Field row can be written using
this method. Syntax:
writerow(fields)
writerows(): This method is used to write multiple rows at a time. This can be used to
write rows list. Syntax:
writerows(rows)
Example:
We can also write dictionary to the CSV file. For this the CSV module provides the
[Link] class. This class returns a writer object which maps dictionaries onto output
rows. Syntax:
[Link](csvfile, fieldnames, restval=”, extrasaction=’raise’, dialect=’excel’, *args,
**kwds)
[Link] provides two methods for writing to CSV. They are:
writeheader(): writeheader() method simply writes the first row of your csv file using
the pre-specified fieldnames. Syntax:
writeheader()
writerows(): writerows method simply writes all the rows but in each row, it writes
only the values(not keys). Syntax:
writerows(mydict)
Example:
2. mainloop(): There is a method known by the name mainloop() is used when your
application is ready to run. mainloop() is an infinite loop used to run the application,
wait for an event to occur and process the event as long as the window is not closed.
[Link]()
import tkinter
m = [Link]()
'''
widgets are added here
'''
[Link]()
Tkinter also offers access to the geometric configuration of the widgets which can organize
the widgets in the parent windows. There are mainly three geometry manager classes class.
1. pack() method:It organizes the widgets in blocks before placing in the parent widget.
2. grid() method:It organizes the widgets in grid (table-like structure) before placing in
the parent widget.
3. place() method:It organizes the widgets by placing them on specific positions
directed by the programmer.
There are a number of widgets which you can put in your tkinter application. Some of the
major widgets are explained below:
1. Button:To add a button in your application, this widget is used. The general syntax is:
w=Button(master, option=value)
master is the parameter used to represent the parent window. There are number of options
which are used to change the format of the Buttons. Number of options can be passed as
parameters separated by commas. Some of them are listed below.
activebackground: to set the background color when button is under the
cursor.
activeforeground: to set the foreground color when button is under the cursor.
bg: to set the normal background color.
command: to call a function.
font: to set the font on the button label.
image: to set the image on the button.
width: to set the width of the button.
height: to set the height of the button.
o Python
import tkinter as tk
r = [Link]()
[Link]('Counting Seconds')
button = [Link](r, text='Stop', width=25, command=[Link])
[Link]()
[Link]()
Canvas: It is used to draw pictures and other complex layout like graphics, text and widgets.
The general syntax is:
w = Canvas(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
bd: to set the border width in pixels.
bg: to set the normal background color.
cursor: to set the cursor used in the canvas.
highlightcolor: to set the color shown in the focus highlight.
width: to set the width of the widget.
height: to set the height of the widget.
o Python
[Link]:It is used to input the single line text entry from the user.. For multi-line text input,
Text widget is used. The general syntax is:
w=Entry(master, option=value)
master is the parameter used to represent the parent window. There are number of options
which are used to change the format of the widget. Number of options can be passed as
parameters separated by commas. Some of them are listed below.
bd: to set the border width in pixels.
bg: to set the normal background color.
cursor: to set the cursor used.
command: to call a function.
highlightcolor: to set the color shown in the focus highlight.
width: to set the width of the button.
height: to set the height of the button.
o Python
Frame: It acts as a container to hold the widgets. It is used for grouping and
organizing the widgets. The general syntax is:
w = Frame(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
highlightcolor: To set the color of the focus highlight when widget has to be
focused.
bd: to set the border width in pixels.
bg: to set the normal background color.
cursor: to set the cursor used.
width: to set the width of the widget.
height: to set the height of the widget.
o Python
root = Tk()
frame = Frame(root)
[Link]()
bottomframe = Frame(root)
[Link]( side = BOTTOM )
redbutton = Button(frame, text = 'Red', fg ='red')
[Link]( side = LEFT)
greenbutton = Button(frame, text = 'Brown', fg='brown')
[Link]( side = LEFT )
bluebutton = Button(frame, text ='Blue', fg ='blue')
[Link]( side = LEFT )
blackbutton = Button(bottomframe, text ='Black', fg ='black')
[Link]( side = BOTTOM)
[Link]()
[Link]: It refers to the display box where you can put any text or image which can be
updated any time as per the code. The general syntax is:
w=Label(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
bg: to set the normal background color.
bg to set the normal background color.
command: to call a function.
font: to set the font on the button label.
image: to set the image on the button.
width: to set the width of the button.
height” to set the height of the button.
o Python
Listbox: It offers a list to the user from which the user can accept any number of
options. The general syntax is:
w = Listbox(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
highlightcolor: To set the color of the focus highlight when widget has to be
focused.
bg: to set the normal background color.
bd: to set the border width in pixels.
font: to set the font on the button label.
image: to set the image on the widget.
width: to set the width of the widget.
height: to set the height of the widget.
o Python
[Link]: It is a part of top-down menu which stays on the window all the time. Every
menubutton has its own functionality. The general syntax is:
w = MenuButton(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
activebackground: To set the background when mouse is over the widget.
activeforeground: To set the foreground when mouse is over the widget.
bg: to set the normal background color.
bd: to set the size of border around the indicator.
cursor: To appear the cursor when the mouse over the menubutton.
image: to set the image on the widget.
width: to set the width of the widget.
height: to set the height of the widget.
highlightcolor: To set the color of the focus highlight when widget has to be
focused.
[Link]: It is used to create all kinds of menus used by the application. The general syntax is:
w = Menu(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of this widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
title: To set the title of the widget.
activebackground: to set the background color when widget is under the
cursor.
activeforeground: to set the foreground color when widget is under the
cursor.
bg: to set the normal background color.
command: to call a function.
font: to set the font on the button label.
image: to set the image on the widget.
o Python
Message: It refers to the multi-line and non-editable text. It works same as that of
Label. The general syntax is:
w = Message(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
bd: to set the border around the indicator.
bg: to set the normal background color.
font: to set the font on the button label.
image: to set the image on the widget.
width: to set the width of the widget.
height: to set the height of the widget.
o Python
[Link]: It is used to offer multi-choice option to the user. It offers several options
to the user and the user has to choose one option. The general syntax is:
w = RadioButton(master, option=value)
There are number of options which are used to change the format of this widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
activebackground: to set the background color when widget is under the
cursor.
activeforeground: to set the foreground color when widget is under the
cursor.
bg: to set the normal background color.
command: to call a function.
font: to set the font on the button label.
image: to set the image on the widget.
width: to set the width of the label in characters.
height: to set the height of the label in characters.
o Python
[Link]: It is used to provide a graphical slider that allows to select any value from that
scale. The general syntax is:
w = Scale(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
cursor: To change the cursor pattern when the mouse is over the widget.
activebackground: To set the background of the widget when mouse is over
the widget.
bg: to set the normal background color.
orient: Set it to HORIZONTAL or VERTICAL according to the requirement.
from_: To set the value of one end of the scale range.
to: To set the value of the other end of the scale range.
image: to set the image on the widget.
width: to set the width of the widget.
o Python
Scrollbar: It refers to the slide controller which will be used to implement listed
widgets. The general syntax is:
w = Scrollbar(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
width: to set the width of the widget.
activebackground: To set the background when mouse is over the widget.
bg: to set the normal background color.
bd: to set the size of border around the indicator.
cursor: To appear the cursor when the mouse over the menubutton.
o Python
Text: To edit a multi-line text and format the way it has to be displayed. The general
syntax is:
w =Text(master, option=value)
There are number of options which are used to change the format of the text. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
highlightcolor: To set the color of the focus highlight when widget has to be
focused.
insertbackground: To set the background of the widget.
bg: to set the normal background color.
font: to set the font on the button label.
image: to set the image on the widget.
width: to set the width of the widget.
height: to set the height of the widget.
o Python
[Link]: This widget is directly controlled by the window manager. It don’t need any
parent window to work [Link] general syntax is:
w = TopLevel(master, option=value)
There are number of options which are used to change the format of the widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
bg: to set the normal background color.
bd: to set the size of border around the indicator.
cursor: To appear the cursor when the mouse over the menubutton.
width: to set the width of the widget.
height: to set the height of the widget.
o Python
Output :
Socket successfully created
there was an error resolving the host
Here when we will be successfully connected the output will be:
Socket successfully created
the socket has successfully connected to google
First of all, we made a socket.
Then we resolved google’s IP and lastly, we connected to google.
Now we need to know how can we send some data through a socket.
For sending data the socket library has a sendall function. This function allows you to
send data to a server to which the socket is connected and the server can also send
data to the client using this function.
A simple server-client program:
Server:
A server has a bind() method which binds it to a specific IP and port so that it can listen to
incoming requests on that IP and port. A server has a listen() method which puts the server
into listening mode. This allows the server to listen to incoming connections. And last a
server has an accept() and close() method. The accept method initiates a connection with the
client and the close method closes the connection with the client.
import smtplib
# creates SMTP session
s = [Link]('[Link]', 587)
# start TLS for security
[Link]()
# Authentication
[Link]("sender_email_id", "sender_email_id_password")
# message to be sent
message = "Message_you_need_to_send"
# sending the mail
[Link]("sender_email_id", "receiver_email_id", message)
# terminating the session
[Link]()
import smtplib
# list of email_id to send the mail
li = ["xxxxx@[Link]", "yyyyy@[Link]"]
for dest in li:
s = [Link]('[Link]', 587)
[Link]()
[Link]("sender_email_id", "sender_email_id_password")
message = "Message_you_need_to_send"
[Link]("sender_email_id", dest, message)
[Link]()
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.
What is 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
To understand the concept of CGI, let us see what happens when we click a hyper link to
browse a particular web page or URL.
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>
The following line should also be added for apache server to treat .py file as cgi script.
AddHandler cgi-script .py
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
Here 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.
print ("Content-type:text/html\r\n\r\n")
print ('<html>')
print ('<head>')
print ('<title>Hello Word - First CGI Program</title>')
print ('</head>')
print ('<body>')
print ('<h2>Hello Word! This is my first CGI program</h2>')
print ('</body>')
print ('</html>')
Note − First line in the script must be the path to Python executable. It appears as a comment
in Python program, but it is called shebang line.
In Linux, it should be #!/usr/bin/python3.
In Windows, it should be #!c:/python311/[Link].
Enter the following URL in your browser −
[Link]
Hello Word! This is my first CGI program
This [Link] script is a simple Python script, which writes its output on STDOUT file, i.e.,
screen. There is one important and extra feature available which is first line to be
printed Content-type:text/html\r\n\r\n. This line is sent back to the browser and it specifies
the content type to be displayed on the browser screen.
By now you must have understood basic concept of CGI and you can write many
complicated CGI programs using Python. This script can interact with any other external
system also to exchange information such as RDBMS.
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
For 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
.
Content-type:
1 A MIME string defining the format of the file being returned. Example is Content-
type:text/html
Expires: Date
The date the information becomes invalid. It is used by the browser to decide when a
2
page needs to be refreshed. A valid date string is in the format 01 Jan 1998 12:00:00
GMT.
Location: URL
3 The URL that is returned instead of the URL requested. You can use this field to
redirect a request to any file.
Last-modified: Date
4
The date of last modification of the resource.
Content-length: N
5 The length, in bytes, of the data being returned. The browser uses this value to report
the estimated download time for a file.
Set-Cookie: String
6
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
.
CONTENT_TYPE
1 The data type of the content. Used when the client is sending attached content to the
server. For example, file upload.
CONTENT_LENGTH
2
The length of the query information. It is available only for POST requests.
HTTP_COOKIE
3
Returns the set cookies in the form of key & value pair.
HTTP_USER_AGENT
4 The User-Agent request-header field contains information about the user agent
originating the request. It is name of the web browser.
PATH_INFO
5
The path for the CGI script.
QUERY_STRING
6
The URL-encoded information that is sent with GET method request.
REMOTE_ADDR
7 The IP address of the remote host making the request. This is useful logging or for
authentication.
REMOTE_HOST
8 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.
REQUEST_METHOD
9 The method used to make the request. The most common methods are GET and
POST.
SCRIPT_FILENAME
10
The full path to the CGI script.
SCRIPT_NAME
11
The name of the CGI script.
SERVER_NAME
12
The server's hostname or IP Address
SERVER_SOFTWARE
13
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
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. The page
and the encoded information are separated by the ? character as follows −
[Link]
The GET method is the default method to pass information from the browser to the
web server and it produces a long string that appears in your browser's Location:box.
Never use GET method if you have password or other sensitive information to pass to
the server.
The GET method has size limtation: only 1024 characters can be sent in a request
string.
The GET method sends information using QUERY_STRING header and will be
accessible in your CGI Program through QUERY_STRING environment variable.
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=Malhar&last_name=Lathkar
Given below is the hello_get.py script to handle the input given by web browser. We are
going to use the cgi module, which makes it very easy to access the passed information −
# 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")
print()
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 Malhar Lathkar
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.
# 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>"
Let us 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>
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 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.
# 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 −
# 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
Maths Submit
Above example has been disabled intentionally to save people uploading file on our server,
but you can try above code with your server.
Here is the script save_file.py to handle file upload −
import cgi, os
import cgitb; [Link]()
form = [Link]()
# Get filename here.
fileitem = form['filename']
# Test if the file was uploaded
if [Link]:
# strip leading path from file name to avoid
# directory traversal attacks
fn = [Link]([Link])
open('/tmp/' + fn, 'wb').write([Link]())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
print """\
Content-Type: text/html\n
<html>
<body>
<p>%s</p>
</body>
</html>
""" % (message,)
If you run the above script on Unix/Linux, then you need to take care of replacing file
separator as follows, otherwise on your windows machine above open() statement should
work fine.
fn = [Link]([Link]("\\", "/" ))
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. This HTTP header is be different
from the header mentioned in previous section.
For example, if you want make a FileName file downloadable from a given link, then its
syntax is as follows −
# 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]()