CGI Used In Python

Python - CGI Programming - Tutorialspoint

CGI Used In Python

Hello friends, Today our tutorial is based on CGI (Common Gateway Interface) because my followers told me we want a tutorial on CGI, how we could use CGI by python programming language.

So, without any delay let’s start our tutorial :-

“ Python is one of the most versatile programming languages available in the software Industries today. The main reason for the popularity of Python is the multiple features it brings to the Developers across multiple platforms because it is better than other languages today.

One of the features of the Python programming system that we get asked about most is the Python CGI and thus in this tutorial, we will talk more about the Python CGI, its uses and how you can implement it in your projects on your company.”

Following contents will be covered in this tutorials,let’s see that :-

·         What is CGI?
·         Structure of a Python CGI Program
·         CGI Environment Variables

What Is CGI?
 Python CGI programming - [PPT Powerpoint]
CGI or Common Gateway Interface is the industry accepted acronym for a set method that is used to define how information is exchanged between the web server and a custom script. As of date, the CGI scripts are officially maintained by the NCSA.

Using CGI in Python
 Scaling Python for High-Load Web Sites
We will mentioned before about What is CGI, CGI is a manner of writing a program that has the ability to exchange data through a web server that is running on another web server.

One of the most important things to note, while writing a CGI program on the Python interface is that CGI programs are written to dynamically generate web pages that not only take input from users but also display the output at the same time.

To understand the concept of CGI programming in Python, let us take a look at the following example.

Note:- You need to have apache2 already installed in order to execute the following example. This program named ‘’ will by default run on host

     Python Programming Bible | Networking, GUI, Email, XML, CGI ...      

import cgi
print("Content-type: text/html")
print("CGI Program! ")

form = cgi.FieldStorage()

if form.getvalue("script"):
name = form.getvalue("script")
print(“ Hello" +script+"! Thanks for using CGI script!")

if form.getvalue("Use CGI"):
print("Iam using CGI!!! ")

if form.getvalue("not use CGI"):
print("not use CGI? ")

print("script: ")
print("Use CGI")
print("not use CGI")

Structure of a Python CGI Program

 CGI Script Vulnerability 'Httpoxy' Allows Man-in-the-Middle Attack ...

Now that you know what a CGI program looks like in Python, let’s see this at ones below :-

The output of a CGI script written in Python must contain two sections separated and explain below :-

·         The first section will contain the headers that describe
·         the second section will contain the kind of data that will be used during the execution of the script.

To understand this, take a look at the example which is given below :-
print ("Content-Type : text/html")

print ("CGI Program")

print ("This is HTML's Body section ")

In order to use the CGI module in Python, you first need to import it into the Python IDLE.

The syntax to do the same is as follows :-

import cgitb


By using the above code, you are basically triggering the formation of a special exception handler that has the ability to display any run time error within the browser at the time of execution.

Legend of Syntaxes

When scripting a CGI program in Python take note of the following commonly used syntaxes.


Content or Type: text/html
Location: URL
Expires: Date
Content Length: N
Set Cookie: String

CGI Environment Variables

 CGI Fresher Job Openings As Python Developer At Hyderabad In ...

Along with the HTMl syntaxes shared above, you also need to make a note about the commonly used

·         CONTENT_TYPE: This is used to describe data and type of the content.
·         CONTENT_LENGTH: This is mainly used in Post to define the length of a query or information.
·         HTTP_COOKIE: If in a certain situation, the user has set a cookie, then this is used to return the same.
·         HTTP_USER_AGENT: If you need to view the type of browser the user is currently using, then this variable will be used.
·         REMOTE_HOST: This is used to define the host name of the visitor.
·         PATH_INFO: This is used to define the path of a CGI script.
·         REMOTE_ADDR: If you need to define the IP address of the visitor, then you can make use of this variable.
·         REQUEST_METHOD: This is used to make a request either via POST or GET.

This is the the end of our tutorial.

Got a question for us? Mention them in the comments section of this tutorial and we will get back to you...

Thank you Guys and Good Bye...

>>> Serialization & Deserialization in C#

>>> Compare two voices by python  

Post a Comment