How to generate webpages using CGI scripts?
Last Updated :
04 Jan, 2024
In this article, we will explore the process of generating webpages using CGI scripts. Furthermore, we will discuss the importance of CGI script implementation in facilitating dynamic content creation. Additionally, we'll delve into the intricacies of linking one page to another by creating multiple pages within Python CGI scripts, ensuring a seamless and well-implemented navigation experience.
What are Webpages?
Webpages are documents or files accessible on the World Wide Web, typically formatted in HTML (Hypertext Markup Language). These documents are displayed in web browsers and can contain a variety of content such as text, images, videos, and hyperlinks. Webpages are the building blocks of websites, and users interact with them to consume information, access services, or engage in various online activities. The content of a webpage is often designed and organized using a combination of HTML, and CSS (Cascading Style Sheets).
Generate Webpages Using CGI Scripts
Here, we will walk through a step-by-step implementation guide on how to generate webpages using CGI scripts. Follow the steps below for generating webpages using Python CGI scripts:
Required Installation
Here, we will install the following things to start with generating webpages using CGI scripts:
- Install Python
- Install Python CGI
Create Folder
First, create a folder named “Webpage” in the “htdocs” directory. Inside this “Webpage” folder, create three files: python1.py, python2.py, and python3.py.
In the “python1.py” file we created the home page, In the “python2.py” file we created the about page and In the “python3.py” file we created the contact page, write a CGI script to retrieve page from one page to other page.

Create a file to Write Python CGI Script
python1.py: Below code is a Python CGI script that generates an HTML webpage. It specifies the Python interpreter's location, sets the content type to HTML, and then prints an HTML document with a title, styling, and content.
The webpage welcomes users to the GeeksforGeeks home page, displaying a brief description and navigation links to "About" and "Contact" pages.
Python3
#!C:\Program Files\Python311\python.exe
print("Content-type: text/html\n\n")
print("""
<!DOCTYPE html>
<html>
<head>
<title>GeeksforGeeks - Home</title>
<style>
body {
font-family: Arial, sans-serif;
}
h2 {
color: #2e8b57; /* GeeksforGeeks green color */
}
</style>
</head>
<body>
<h2>Welcome to GeeksforGeeks - Home Page</h2>
<p>This is the home page content.</p>
<p><a href="python2.py">About</a> | <a href="python3.py">Contact</a></p>
</body>
</html>
""")
Output:

python2.py: This Python CGI script generates an HTML webpage for the "About" section of GeeksforGeeks. It specifies the Python interpreter's location, sets the content type to HTML, and then prints an HTML document with a title, styling, and content.
The webpage provides information about GeeksforGeeks, including a brief description and navigation links to the "Home" and "Contact" pages.
Python3
#!C:\Program Files\Python311\python.exe
print("Content-type: text/html\n\n")
print("""
<!DOCTYPE html>
<html>
<head>
<title>GeeksforGeeks - About</title>
<style>
body {
font-family: Arial, sans-serif;
}
h2 {
color: #2e8b57; /* GeeksforGeeks green color */
}
</style>
</head>
<body>
<h2>About GeeksforGeeks</h2>
<p>This is the about page content.</p>
<p><a href="python1.py">Home</a> | <a href="python3.py">Contact</a></p>
</body>
</html>
""")
Output

python3.py : This Python CGI script generates an HTML webpage for the "Contact" section of GeeksforGeeks. It specifies the Python interpreter's location, sets the content type to HTML, and then prints an HTML document with a title, styling, and content.
The webpage includes information about contacting GeeksforGeeks and provides navigation links to the "Home" and "About" pages.
Python3
#!C:\Program Files\Python311\python.exe
print("Content-type: text/html\n\n")
print("""
<!DOCTYPE html>
<html>
<head>
<title>GeeksforGeeks - Contact</title>
<style>
body {
font-family: Arial, sans-serif;
}
h2 {
color: #2e8b57; /* GeeksforGeeks green color */
}
</style>
</head>
<body>
<h2>Contact GeeksforGeeks</h2>
<p>This is the contact page content.</p>
<p><a href="python1.py">Home</a> | <a href="python2.py">About</a></p>
</body>
</html>
""")
Output

Configuration and Start the Xampp Server
You can refer Create a CGI Script for complete configuration and how we can start the server to run out CGI Script.
Run the CGI Script
In this step, we will run the CGI Script by using the following command in your web browser
http://127.0.0.1/Webpage/python1.py
Video Demonstration
Conclusion
In conclusion, this step-by-step guide has provided insights into the process of generating webpages using CGI scripts. By setting up the server environment, creating a CGI script, and configuring permissions, users can dynamically generate HTML content for their webpages. The flexibility of CGI scripts allows for the incorporation of dynamic elements and the seamless handling of form submissions, enhancing the overall interactivity of web content. Embracing CGI scripts empowers developers to create dynamic, responsive, and engaging webpages, contributing to a more interactive and user-friendly online experience.
Similar Reads
How to Create Form using CGI script
HTML forms are a fundamental part of web development, allowing users to input data and interact with web applications. Web developers often use Common Gateway Interface (CGI) scripts to process and send the data submitted through these forms. In this article, we'll explain what Python CGI scripts ar
3 min read
How to generate basic HTML output using CGI scripts?
In this article, we will explore the process of generating basic HTML output using CGI scripts. Specifically, we will focus on creating a simple HTML web page using Python. The objective is to provide a step-by-step walkthrough, detailing the entire process of creating and utilizing CGI scripts to g
3 min read
How to create a simple CGI script?
In this article, we will explore how to create a simple CGI (Common Gateway Interface) script on a Windows machine but before that, we need some basic ideas about these technologies ( CGI Script, HTTP, Web Server ).CGI Script: A CGI script is a program that runs on a web server and generates dynamic
2 min read
How to Clone webpage Using pywebcopy in Python?
Sometimes we need a handy web page on your local hard drive. So, here we are going to write a simple Python script to Scrap a web page. Web scraping is used for extracting data from websites for offline reading, storage, or whatever reason. Before writing the script we need to know pywebcopy. pywebc
1 min read
How to print a web page using JavaScript ?
Javascript is the world most popular lightweight, cross-platform, interpreted compiled programming language, along with a scripting language for web. It facilitates the dynamic functionality that helps to make the interactive website. As all the Morden browsers use the Javascript & is a client-s
2 min read
How to get the source code of a web page using PHP ?
Given a webpage, for which we need to find its source code using PHP. For this, we are going to use the PHP htmlspecialchars() function which converts any predefined characters to their subsequent HTML entities.Example 1: Suppose we take a sample website that looks like the below image, let us see w
2 min read
Extract all the URLs from the webpage Using Python
Scraping is a very essential skill for everyone to get data from any website. In this article, we are going to write Python scripts to extract all the URLs from the website or you can save it as a CSV file. Module Needed: bs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and
2 min read
Random Image Generator using JavaScript
In this article, we will learn how to generate random images in fixed intervals using only HTML, CSS, and JavaScript.Approach: The pictures which are going to be generated randomly on the website should be stored in the form of an array, these pictures are then displayed to the user within a regular
4 min read
How to Build a Website using HTML?
Building a website using HTML (Hypertext Markup Language) is the foundation of web development. HTML allows you to structure content, define headings, paragraphs, lists, and links, and create visually appealing web pages.In this article, we'll learn the fundamentals of How to build a Website using H
5 min read
Python script to monitor website changes
In this article, we are going to discuss how to create a python script to monitor website changes. You can code a program to monitor a website and it will notify you if there are any changes. This program has many useful scenarios for example if your school website has updated something you will com
3 min read