28.10.2021 Views

Python Tutorial ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

If you click hello.py, then this produces the following output:

Content-type:text/html

Hello Word! This is my first CGI program

This hello.py script is a simple Python script, which is writing 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 specify the content type to be displayed on the browser screen.

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 exertnal 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

For 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.

Header

Content-type:

Expires: Date

Location: URL

Last-modified: Date

Content-length: N

Description

A MIME string defining the format of the file being returned. Example is Contenttype:text/html

The date the information becomes invalid. This should be used by the browser to

decide when a page needs to be refreshed. A valid date string should be in the

format 01 Jan 1998 12:00:00 GMT.

The URL that should be returned instead of the URL requested. You can use this

field to redirect a request to any HTML 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-Cookie: String

CGI Environment Variables

Set the cookie passed through the string

All the CGI program will have access to the following environment variables. These variables play an important

role while writing any CGI program.

Variable Name

CONTENT_TYPE

CONTENT_LENGTH

HTTP_COOKIE

Description

The data type of the content. Used when the client is sending attached content to

the server. For example, file upload, etc.

The length of the query information. It's available only for POST requests.

Returns the set cookies in the form of key & value pair.

TUTORIALS POINT

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!