Python is one of the most versatile programming languages available in the market today. The main reason for popularity of Python is the great number of features it brings to the table as well as its versatility across multiple platforms. One of the features of the Python programming system that we get asked about most is the Python CGI and thus in this article we will talk more about the Python CGI, its uses and how you can implement it in your day to day coding.
Following pointers will be covered in this article,
Python CGI
What Is CGI?
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
As mentioned in an earlier paragraph, 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.
Example
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 ‘hello.py’ will by default run on host 127.0.0.1.
#!/usr/bin/python3 # Importing the 'cgi' module import cgi print("Content-type: text/htmlrnrn") print("<html><body>") print(" <h1> Hello Program! </h1> ") # Using the inbuilt methods form = cgi.FieldStorage() if form.getvalue("name"): name = form.getvalue("name") print(" <h1>Hello" +name+"! Thanks for using my script!</h1> ") if form.getvalue("happy"): print(" Yayy! I'm happy too! ") if form.getvalue("sad"): print(" Oh no! Why are you sad? ") # Using HTML input and forms method print(" <form method='post' action='hello2.py'> ") print(" Name: <input type='text' name='name' /> ") print("<input type='checkbox' name='happy' /> Happy") print("<input type='checkbox' name='sad' /> Sad") print("<input type='submit' value='Submit' />") print("</form") print("</body></html>")
Output
Let us see what is the structure of a Python CGI program,
Structure of a Python CGI Program
Now that you know what a CGI program looks like in Python, let us take a closer look at the structure of the same.
- The output of a CGI script written in Python must contain two sections separated by a blank line.
- The first section will contain the headers that describe and 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 below.
print ("Content-Type : text/html") # then comes the rest hyper-text documents print ("<html>") print ("<head>") print ("<title>My First CGI-Program </title>") print ("<head>") print ("<body>") print (" <h3>This is HTML's Body section </h3> ") print ("</body>") print ("</html>")
Output
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
cgitb.enable()
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.
HTML
- Content or Type: text/html
- Location: URL
- Expires: Date
- Content Length: N
- Set Cookie: String
Let us take a look at the final bit of this Python CGI article that is Environment Variables,
CGI Environment Variables
Along with the HTMl syntaxes shared above, you also need to make a note about the commonly used CGI environment variables.
- 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 brings us to the end of this article.
To get in-depth knowledge on Python along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.
Got a question for us? Mention them in the comments section of this article and we will get back to you.