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
What 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\n\r\n 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\n\r\n
Common HTTP Headers in CGI
Here are the most frequently used HTTP headers in Python CGI programming ?
| Sr.No. | Header | Description |
|---|---|---|
| 1 | Content-type: |
A MIME string defining the format of the file being returned. Example: Content-type: text/html
|
| 2 | Expires: Date |
The date the information becomes invalid. Used by the browser to decide when a page needs refreshing. Format: 01 Jan 1998 12:00:00 GMT
|
| 3 | Location: URL |
The URL that is returned instead of the URL requested. Used to redirect requests to any file |
| 4 | Last-modified: Date |
The date of last modification of the resource |
| 5 | Content-length: N |
The length, in bytes, of the data being returned. Browser uses this to estimate download time |
| 6 | Set-Cookie: String |
Sets a cookie that will be passed through the string |
Example Usage
Here's how you would use these headers in a Python CGI script ?
#!/usr/bin/env python3
# Content-type header
print("Content-type: text/html\r\n\r\n")
# HTML content
print("<html>")
print("<head><title>CGI Example</title></head>")
print("<body>")
print("<h1>Hello from CGI!</h1>")
print("</body>")
print("</html>")
Conclusion
HTTP headers are crucial for proper CGI communication between server and browser. The Content-type header is mandatory, while others like Location and Set-Cookie provide additional functionality for redirects and session management.
