- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
#!/usr/bin/python print "Content-type:text/html\r \r " 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>'
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 \r . 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.
The line Content-type:text/html\r \r 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
Content-type: text/html\r \r
There are few other important HTTP headers, which you will use frequently in your CGI Programming.
|Sr.No.||Header & Description|
A MIME string defining the format of the file being returned. Example is Content-type:text/html
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.
The URL that is returned instead of the URL requested. You can use this field to redirect a request to any file.
The date of last modification of the resource.
The length, in bytes, of the data being returned. The browser uses this value to report the estimated download time for a file.
Set the cookie passed through the string
- 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
- What is CGI in Python?
- CGI Environment Variables in Python
- Passing Drop Down Box Data to CGI Program in Python
- Using Cookies in CGI in Python
- What Content-type is required to write Python CGI program?
- How to write Python CGI program to interact with MySQL?
- 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?