A Python CGI script runs on a web server and sends dynamic HTML content to the browser. To send results from your CGI script to the browser, you need to output proper HTTP headers followed by HTML content. Basic CGI Response Structure Every CGI script must start with a content-type header, followed by a blank line, then the HTML content − #!/usr/bin/env python3 import cgi # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from HTML form fields first_name = form.getvalue('first_name') last_name = form.getvalue('last_name') # Send HTTP header print("Content-type:text/html") print() ... Read More
When working with Python CGI scripts, you often need to access HTTP headers sent by the client. Apache's mod_cgi automatically converts HTTP headers into environment variables with an HTTP_ prefix. How Apache Handles HTTP Headers Apache's mod_cgi sets environment variables for each HTTP request header received. The variables follow a specific pattern: All header names get an HTTP_ prefix Header names are converted to uppercase Hyphens are replaced with underscores For example, x-client-version: 1.2.3 becomes HTTP_X_CLIENT_VERSION. Reading a Specific Header To read a specific custom header, use os.environ with the converted header ... Read More
CGI (Common Gateway Interface) allows Python scripts to handle web requests and interact with databases like MySQL. This tutorial shows how to create a login system using Python CGI that validates user credentials against a MySQL database. HTML Login Form First, create an HTML form that collects user credentials and submits them to the Python CGI script ? login.html Email: Password: ... Read More
Python CGI (Common Gateway Interface) scripts allow you to run Python programs on a web server to generate dynamic web content. By default, Apache doesn't execute Python scripts, so you need to configure it properly. Prerequisites Before configuring Apache for Python CGI, ensure you have: Apache web server installed Python installed on your system Access to Apache configuration files Step 1: Configure Apache httpd.conf File Open the Apache configuration file httpd.conf (usually located in /etc/apache2/ or /etc/httpd/conf/) and add the following configurations: # Enable CGI module LoadModule cgi_module modules/mod_cgi.so # ... Read More
To upload a file using Python CGI programming, the HTML form must have the enctype attribute set to multipart/form-data. The input tag with file type creates a "Browse" button that allows users to select files from their system. HTML Upload Form First, create an HTML form that allows file selection − File: This form displays a file selection interface − File: Choose file Upload Python CGI Upload Handler Here's ... Read More
Retrieving cookies in Python CGI programming allows you to access data stored in the user's browser. Cookies are stored in the CGI environment variable HTTP_COOKIE and can be parsed to extract individual values. Cookie Storage Format Cookies are stored in the HTTP_COOKIE environment variable in the following format − key1=value1;key2=value2;key3=value3.... Basic Cookie Retrieval Here's how to retrieve and parse cookies from the environment variable − #!/usr/bin/env python3 # Import modules for CGI handling import os import cgi # Check if cookies exist if 'HTTP_COOKIE' in os.environ: ... Read More
Cookies in Python CGI programming allow you to store small pieces of data on the client's browser. Setting cookies is straightforward − they are sent as HTTP headers before the Content-type field. Setting Basic Cookies Cookies are set using the Set-Cookie HTTP header. Here's how to set UserID and Password cookies ? #!/usr/bin/python3 print("Set-Cookie:UserID=XYZ;\r") print("Set-Cookie:Password=XYZ123;\r") print("Content-type:text/html\r\r") print("") print("Cookies have been set!") print("") Setting Cookies with Attributes You can specify additional attributes like expiration date, domain, and path for better cookie control ? #!/usr/bin/python3 print("Set-Cookie:UserID=XYZ;\r") print("Set-Cookie:Password=XYZ123;\r") print("Set-Cookie:Expires=Tuesday, 31-Dec-2025 23:12:40 GMT;\r") print("Set-Cookie:Domain=www.tutorialspoint.com;\r") ... Read More
HTTP protocol is a stateless protocol, meaning it doesn't remember information between requests. For commercial websites, it's essential to maintain session information across different pages. For example, user registration may span multiple pages, requiring session data persistence. In many situations, using cookies is the most efficient method of remembering and tracking preferences, purchases, commissions, and other information required for better visitor experience or site statistics. How Cookies Work Your server sends data to the visitor's browser in the form of a cookie. The browser may accept the cookie and store it as a plain text record on ... Read More
In Python CGI Programming, the two primary methods for passing information from a web browser to a web server are GET and POST. Understanding their differences is crucial for web development security and functionality. GET Method The GET method sends encoded user information appended to the page request. The page and the encoded information are separated by the ? character as follows − http://www.test.com/cgi-bin/hello.py?key1=value1&key2=value2 The GET method is the default method to pass information from browser to web server. It produces a long string that appears in your browser's Location box. Never use GET ... Read More
HTTP headers are essential components in Python CGI programming that provide metadata about the response being sent to the browser. They define how the browser should interpret and handle the content. HTTP Header Format The line Content-type:text/html\r\r is part of HTTP header which is sent to the browser to understand the content. All HTTP headers follow this format ? HTTP Field Name − Field Content Example Content-type: text/html\r\r Common HTTP Headers in CGI Here are the most frequently used HTTP headers in Python CGI programming ? Sr.No. Header ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance