- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
First CGI Program in Python
Here is a simple link, which is linked to a CGI script called hello.py. This file is kept in /var/www/cgi-bin directory and it has following content. Before running your CGI program, make sure you have change mode of file using chmod 755 hello.py UNIX command to make file executable.
Example
#!/usr/bin/python print "Content-type:text/html\r\n\r\n" print '</html>' print '</head>' print '</title>Hello Word - First CGI Program<//title>' print '<//head>' print '</body>' print '</h2>Hello Word! This is my first CGI program<//h2>' print '<//body>' print '<//html>'
Output
If you click hello.py, then this produces the following output −
Hello Word! This is my first CGI program
This hello.py script is a simple Python script, which writes its output on STDOUT file, i.e., screen. There is one important and extra feature available which is first line to be printed Content-type:text/html\r\n\r\n. This line is sent back to the browser and it specifies the content type to be displayed on the browser screen.
By now you must have understood basic concept of CGI and you can write many complicated CGI programs using Python. This script can interact with any other external system also to exchange information such as RDBMS.
HTTP Header
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 the HTTP header will be in the following form −
HTTP Field Name: Field Content
Example
Content-type: text/html\r\n\r\n
There are few other important HTTP headers, which you will use frequently in your CGI Programming.
Sr.No. | Header & Description |
---|---|
1 | Content-type: A MIME string defining the format of the file being returned. Example is Content-type:text/html |
2 | Expires: Date The date the information becomes invalid. It is used by the browser to decide when a page needs to be refreshed. A valid date string is in the format 01 Jan 1998 12:00:00 GMT. |
3 | Location: URL The URL that is returned instead of the URL requested. You can use this field to redirect a request 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. The browser uses this value to report the estimated download time for a file. |
6 | Set-Cookie: String Set the cookie passed through the string |
- Related Articles
- First CGI Program using Perl
- How to write first Hello World program using CGI programming in Python?
- Passing Checkbox Data to CGI Program in Python
- Passing Radio Button Data to CGI Program in Python
- Passing Text Area Data to CGI Program in Python
- Passing Drop Down Box Data to CGI Program in Python
- What is CGI in Python?
- CGI Environment Variables in Python
- What Content-type is required to write Python CGI program?
- How to write Python CGI program to interact with MySQL?
- Using Cookies in CGI in Python
- How to do CGI programming in Python?
- How do cookies work in Python CGI Programming?
- How to setup cookies in Python CGI Programming?
- How to retrieve cookies in Python CGI Programming?
