CGI stands for Common Gateway Interface in Python which is a set of standards that explains how information or data is exchanged between the web server and a routine script. This interface is used by web servers to route information requests supplied by a browser or we can say that CGI is customary for external gateway programs to interface with information servers such as HTTP servers.
A CGI script is invoked by an HTTP server, usually to course user input which is submitted through an HTML <FORM> or an <ISINDEX> element.
Concept of CGI
Whenever we click on a hyperlink to browse a particular web page or URL, our browser interacts with the HTTP web server and asks for the same URL (or filename). Web Server then parses the URL and looks for the same filename. If that file is found, then that file is sent back to the browser, otherwise, an error message is sent indicating that we are demanding the wrong file. Web browser takes the response from a web server and displays it, then whether it is the received file from the webserver or an error message. But, conversely, it is possible to set up the HTTP server so that whenever a specific file is requested, then that file is not sent back, but instead, it is executed as a program, and whatever that program output is, that is sent back to our browser for display. This same function is called the Common Gateway Interface (or CGI) and the programs which are executed are called CGI scripts. In python, these CGI programs are Python Script.
Architecture of CGI:
Example:
Let us take a sample URL which passes two values to first_cgi.py program using GET method:
/cgi-bin/first_cgi.py?your_name=Piyush&company_name=GeeksforGeeks
Below is the first_cgi.py script to handle the input given by the above sample URL. Here we will use cgi module which will makes it very easy to access the passed information
Python3
#!/usr/bin/python
# Import CGI and CGIT module
import cgi, cgitb
# to create instance of FieldStorage
# class which we can use to work
# with the submitted form data
form = cgi.FieldStorage()
your_name = form.getvalue('your_name')
# to get the data from fields
comapny_name = form.getvalue('company_name')
print ("Content-type:text/html\n")
print ("<html>")
print ("<head>")
print ("<title>First CGI Program</title>")
print ("</head>")
print ("<body>")
print ("<h2>Hello, %s is working in %s</h2>"
% (your_name, company_name))
print ("</body>")
print ("</html>")
Output:
Hello, Piyush is working in GeeksforGeeks
Similar Reads
What is Python Interpreter Well if you are new to Python you have come across the word interpreters but before knowing about Python interpreters, we need to what is an interpreter, what it will do, the advantages and disadvantages of the interpreter, and so on. Well in this handy article, we will cover the Importance of Inter
4 min read
CGI Programming in Python What is CGI? Common Gateway Interface (also known as CGI) is not a kind of language but just a specification(set of rules) that helps to establish a dynamic interaction between a web application and the browser (or the client application). The CGI programs make possible communication between client
5 min read
GET and POST in Python CGI In this article, we will explore the GET and POST methods in CGI. In Python, the Common Gateway Interface (CGI) plays an important role as it enables and helps in the creation of dynamic web pages. It basically allows Python scripts to be executed and run on a web server in response to a client's HT
5 min read
What is Python? Its Uses and Applications Python is a programming language that is interpreted, object-oriented, and considered to be high-level. What is Python? Python is one of the easiest yet most useful programming languages and is widely used in the software industry. People use Python for Competitive Programming, Web Development, and
8 min read
Upload files in Python In this article, we will be looking into the process of file uploading in Python using cgi environment. One often comes across various web applications in which the client or the users is required to upload data in the form of a file(eg. image file, an audio file, text file, etc). There are two aspe
3 min read