How to generate basic HTML output using CGI scripts?
Last Updated :
04 Jan, 2024
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 generate HTML content within a web environment.
What is HTML Output?
HTML output is the formatted content generated in Hypertext Markup Language (HTML), the standard language for web page creation. In scripting, particularly with CGI scripts, HTML output is dynamically generated and sent to web browsers as a response, allowing for interactive and customized web content. It includes elements like text, images, forms, and links, shaping the visual presentation of web pages.
Generate Basic HTML Output Using CGI Scripts
Here, we will provide a step-by-step guide on how to generate basic HTML output using Python CGI scripts in Python.
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 “GeeksforGeeks” in the “htdocs” directory. Inside this “GeeksforGeeks” folder, create one file “python.py”. In the “python.py” file, write a CGI script to show the html output on the web page.

Write Python CGI Script
The provided Python script is a CGI (Common Gateway Interface) program that generates a simple HTML webpage. It begins by specifying the Python interpreter's path. The script retrieves form data, specifically the value associated with the 'name' parameter. Subsequently, it outputs an HTML document with a title, styling using CSS, and a card container displaying a success message. The success message includes the user-entered name and a smile emoji. The CSS styling defines a card with specific width, padding, margin, and border properties. The script demonstrates the use of CGI to handle input from a web form and dynamically generate an HTML response for display on a web page.
Python3
#!/usr/bin/env python
print("Content-type: text/html\n\n")
import cgi
# Retrieve form data
form = cgi.FieldStorage()
# Extract values from the form
name = form.getvalue('name')
# Generate HTML response
print("<html>")
print("<head>")
print("<title>CGI Script Output</title>")
print("<style>")
print(" /* Style for the card container */")
print(" .card {")
print(" width: 300px;")
print(" padding: 20px;")
print(" margin: 20px;")
print(" border: 1px solid #ccc;")
print(" border-radius: 5px;")
print(" text-align: center;")
print(" }")
print("h2 {")
print(" color: green;")
print("}")
print("</style>")
print("</head>")
print("<body>")
print("<div class='card'>")
print("<h2>GeeksforGeeks</h2>")
print("<p>Success! You've Created {}! 😄</p>".format(name))
print("</div>")
print("</body>")
print("</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/GeeksforGeeks/html.html
Video Demonstration
Conclusion
In conclusion, generating basic HTML output using CGI scripts involves several key steps. First, create a directory within the server's root, often named "htdocs" or similar. Within this directory, establish a folder for your project and create a Python CGI script, ensuring it is executable and properly configured with the correct shebang line. The script should utilize the Common Gateway Interface (CGI) module to retrieve form data and dynamically generate HTML content. This content can include CSS styling for improved presentation.
Similar Reads
How to generate webpages using CGI scripts?
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
4 min read
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 design runtime generated PDF via HTML ?
Runtime-generated PDFs via HTML refer to creating PDF documents on-the-fly using HTML content. This process involves rendering HTML elements into a PDF format during application execution, allowing dynamic data and layouts to be included, which is useful for generating reports, invoices, or other do
4 min read
Random Quote Generator Using HTML, CSS and JavaScript
A Random Quote Generator is capable of pulling quotes randomly from an API, a database, or simply from an array. We will be designing a Random Quote Generator from scratch using HTML, CSS, JavaScript, and type.fit API. The webpage displays a random quote from a collection and upon the click of a but
8 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 Enable or Disable CGI Scripts in Apache?
This article will guide you on how to enable or disable CGI scripts in Apache. Configuring CGI scripts in Apache is a crucial aspect of managing dynamic content generation on web servers. The Common Gateway Interface (CGI) provides a standardized protocol for executing programs, allowing websites to
4 min read
How to Display HTML Tags as Plain Text using PHP?
HTML tags begin with the less-than character and end with greater-than character, the text inside the tag formatted and presented according to the tag used. Every tag has special meaning to the browser but there are cases when to show plain HTML code in webpage. There are various methods in PHP to s
3 min read
How to print on browser's console using PHP ?
The echo command is used in PHP to print any value to the HTML document. Use <script> tag inside echo command to print to the console. Syntax: echo "This is GeeksforGeeks Tutorial."; echo variable_name; Note: The echo command will print the HTML document. This feature can be used to put JavaSc
2 min read
How to Convert Special Characters to HTML in JavaScript?
In JavaScript, special characters like less-than (<), greater-than (>), and others can cause rendering issues in HTML because they are interpreted as tags. To display these characters correctly in HTML, it's necessary to convert them into their respective HTML entities. This process prevents t
2 min read
Create your own Lorem ipsum using HTML CSS and JavaScript
In this article, we are going to implement a Lorem Ipsum Generator Application through HTML, CSS, and JavaScript. Lorem Ipsum, a placeholder text commonly utilized in the printing and typesetting industry, serves to visually represent a document's layout instead of relying on meaningful content.Fina
5 min read