Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to read all HTTP headers in Python CGI script?
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 MoreHow to write Python CGI program to interact with MySQL?
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 MoreHow to execute Python CGI Script on Apache Server?
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 MoreHow do we do a file upload using Python CGI Programming?
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 MoreHow to retrieve cookies in Python CGI Programming?
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 MoreHow to setup cookies in Python CGI Programming?
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 MoreHow do cookies work in Python CGI Programming?
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 MoreWhat is the difference between GET and POST in Python CGI Programming?
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 MoreWhat are important HTTP headers to be frequently used in Python CGI Programming?
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 MoreWhat Content-type is required to write Python CGI program?
When writing Python CGI programs, you must specify a Content-type header to tell the browser how to interpret the response. This is essential for proper web communication between your CGI script and the client's browser. Basic Content-type Header The first line of any CGI program output must be a Content-type header followed by a blank line ‒ #!/usr/bin/env python3 print("Content-type: text/html\r\r") print("") print("Hello CGI") print("") print("Hello from Python CGI!") print("") print("") Why the Content-type Header is Required The Content-type header tells the browser: What type of content to expect (HTML, ...
Read More