Common Gateway Interface: Understanding the Dynamic Content System and Executing CGI Scripts in Python
Are you curious about how dynamic webpages work? Have you ever encountered a website that gave you a personalized experience based on your choices or requested information?
If your answer is yes, then you have come across a website that uses the Common Gateway Interface (CGI). CGI is a protocol that enables web servers to interact with external programs, allowing them to generate dynamic content.
In this article, we will explore the fundamentals of CGI, its benefits and drawbacks, as well as a step-by-step guide on executing a CGI script in Python. What is Common Gateway Interface, and How Does It Work?
CGI is a programming protocol that enables web servers to interact with external programs to generate dynamic content. Since its invention in the early 1990s, the CGI has become a fundamental part of most web applications.
This protocol allows web servers to execute scripts or programs based on various conditions, such as user input, server variables, and file selection. CGI scripts can be written in various programming languages, such as Perl, Python, Ruby, and PHP.
One of the primary functions of CGI is to provide a way for servers to generate dynamic content or webpages on-demand. By using an external program, web servers can generate web pages that are customized to the user’s preferences or requests.
For example, when a user fills an online form, CGI scripts can process the data and generate a custom response that suits the user’s selections.
Advantages and Disadvantages of CGI
CGI is a simple and powerful tool that enables developers to create web applications that generate dynamic content. However, it has some advantages and disadvantages that developers should consider before using it.
Advantages of CGI:
- Support for various programming languages
- Easy to learn and implement
- High compatibility with most web servers and operating systems
- Suitable for small-scale web applications
Disadvantages of CGI:
- Slow performance compared to web frameworks like Django or Flask
- Limited scalability for large web applications
- Susceptible to security risks such as SQL injection and code injection
Executing a Script through CGI
Setting Up a Special Folder for CGI Scripts:
To execute CGI scripts, we need to set up a special folder on our webserver known as the CGI bin folder. This folder is typically located in the root directory of a website and is identified by its name ‘cgi-bin.’ To set up a cgi-bin folder, follow these steps:
- Log in to your webserver using SSH or FTP.
- Navigate to the root directory of your website.
- Create a new folder and name it ‘cgi-bin.’
- Change the folder permissions to 755 using the ‘chmod’ command.
Creating a Python Script and Making It Executable:
In this example, we will create a simple Python script that displays a message when executed. To create the script, follow these steps:
- Open a new file in a text editor and save it as ‘hello.py.’
- Add the following lines of code in the file:
Hello, World!“) ” aria-label=”Copy” data-copied-text=”Copied!” data-has-text-button=”textSimple” data-inside-header-type=”none” aria-live=”polite”>Copy
#!/usr/bin/env python print("Content-Type: text/html") print() print("
Hello, World!
") - Save the file and upload it to the cgi-bin folder on your server.
- Change the file permissions to 755 using the ‘chmod’ command.
Running the Server and Accessing the CGI Script from a Web Browser:
- Open a terminal and enter the following command:
Copy
python -m http.server --cgi 8000
- Open a web browser and navigate to the following URL:
Copy
http://localhost:8000/cgi-bin/hello.py
- Press enter, and you should see the message “Hello, World!” displayed in your web browser.
Modifying a CGI Script
Once you have created a basic CGI script, you can modify it to add more functionality and make it more interactive. One way to do so is by picking a random background color and displaying environment variables to the user.
In this section, we will explore how to modify a CGI script to achieve these objectives.
Adding Functionality to the CGI Script:
To add new functionality to our CGI script, we can modify the existing code or use a different programming language.
In this example, we will use the Python programming language to pick a random background color and display environment variables.
- Modify the existing code to include the random color and environment variables as follows:
“) print(““) print(“
Hello, World! “) print(‘‘ % background_color) print(““) print(““) print(“Welcome to Hello World CGI
“) print(“Here are some environment variables:
“) print(“- “)
for key in os.environ.keys():
print(“
- {}: {} “.format(key, os.environ[key])) print(“