Python 3 – CGI Programming
CGI programming is a way to run Python scripts from a web browser. When we make a request to a website, the request is sent to a server. The server processes the request and sends a response back to the browser. In CGI programming, Python scripts are run on the server to process the request and generate a response.
The Common Gateway Interface (CGI) is a standard for running web applications on a web server. CGI scripts can be written in many programming languages including Python. In this article, we will learn how to write CGI scripts in Python 3.
Requirements
Before we start writing Python CGI scripts, we need to make sure that we have access to a web server that supports CGI. We also need to make sure that Python 3 is installed on the server.
First Python CGI Program
Let’s start by creating our first Python CGI program. Create a file named hello.py
with the following code:
#!/usr/bin/env python3
print("Content-type: text/html\n")
print("<html><head><title>Hello World</title></head><body>")
print("<h1>Hello World</h1>")
print("</body></html>")
The first line tells the server what interpreter to use to execute the script. The print("Content-type: text/html\n")
line specifies that we are sending HTML content to the browser. The HTML code in the print
statements generates a simple web page that says “Hello World”.
Save the file in the web server’s CGI directory, typically /usr/lib/cgi-bin, and make the script executable by running the following command:
chmod +x hello.py
Now we can access our script on the web by visiting http://<server-name>/cgi-bin/hello.py
. We should see the “Hello World” message displayed in our web browser.
Reading Form Data
CGI scripts are often used to process form data that is submitted by a user. Let’s write a Python CGI script that reads form data.
Create a file named form.py
with the following code:
#!/usr/bin/env python3
import cgi
form = cgi.FieldStorage()
print("Content-type: text/html\n")
print("<html><head><title>Form Results</title></head><body>")
print("<h1>Form Results</h1>")
if "name" in form:
print("Name: " + form["name"].value + "<br>")
else:
print("Name not found in form<br>")
if "email" in form:
print("Email: " + form["email"].value + "<br>")
else:
print("Email not found in form<br>")
print("</body></html>")
This script imports the cgi
module and uses it to read the form data that was submitted by the user. The FieldStorage()
function parses the form data and returns a dictionary of keys and values.
The script then checks if the keys “name” and “email” are in the form data dictionary. If they are, it prints the values to the web page. If they are not, it prints an error message.
Save the file in the web server’s CGI directory and make it executable using the command we used previously.
Now we can create an HTML form that we can submit to our Python CGI script. Create a file named form.html
with the following code:
<html>
<head>
<title>Form Example</title>
</head>
<body>
<h1>Form Example</h1>
<form action="/cgi-bin/form.py" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
This HTML code creates a form that we can fill out with our name and email address. The action
attribute specifies that the form should be submitted to our Python CGI script. When we submit the form, we should see the results displayed in our web browser.
Conclusion
Python 3 is a powerful language for CGI programming. It can be used to create dynamic web pages that respond to user input. By learning how to write Python CGI scripts, we can create web applications that are easily accessible from any web browser.