Data Science with Python Certification Traini ...
- 93k Enrolled Learners
- Live Class
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,
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.
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>")
Now that you know what a CGI program looks like in Python, let us take a closer look at the structure of the same.
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>")
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.
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.
Along with the HTMl syntaxes shared above, you also need to make a note about the commonly used CGI environment variables.
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.