Python chat--cgi

Source: Internet
Author: User
Tags html form http cookie

CGI is an interface standard between an external application (CGI program) and a Web server, which is the process of passing information between a CGI program and a Web server. The CGI specification allows the Web server to execute external programs and send their output to a Web browser, CGI turning a simple set of static hypermedia documents from the Web into a complete new interactive media.

The vast majority of CGI programs are used to interpret the input information from the form, to generate the appropriate processing on the server, or to feed the corresponding feedback to the browser. CGI programs make Web pages interactive.

The HTTP header format is as follows:

HTTP 字段名: 字段内容例如Content-type: text/html\r\n\r\n

The following table describes the frequently used information in the HTTP header in CGI programs:

Head Description
Content-type: The MIME information for the request that corresponds to the entity. Example: content-type:text/html
Expires:date Date and time when the response expires
Location:url Used to redirect the receiver to the location of the non-request URL to complete the request or identify the new resource
Last-modified:date Requested last modified time for resource
Content-length:n The requested content length
Set-cookie:string Set HTTP Cookies
CGI Environment variables

All CGI programs receive the following environment variables, which play an important role in CGI programs:

Variable name Description
Content_Type The value of this environment variable indicates the MIME type of the information being passed. Currently, the environment variable content_type is generally: application/x-www-form-urlencoded, who says the data comes from an HTML form.
Content_length If the server and CGI program information is passed as post, this environment variable can be read from the standard input stdin bytes of valid data. This environment variable must be used when reading the input data.
Http_cookie The COOKIE content within the client.
Http_user_agent Provides customer browser information that contains a version number or other proprietary data.
Path_info The value of this environment variable represents the other path information immediately following the CGI program name. It often appears as a parameter to a CGI program.
Query_string If the server and CGI program information is passed in a get, the value of this environment variable even if the information is passed. This information is followed by the CGI program name, with a question mark '? ' in between. Separated.
Remote_addr The value of this environment variable is the IP address of the client sending the request, such as 192.168.1.67 above. This value is always present. And it is a unique identifier that Web clients need to provide to the Web server, which can be used in a CGI program to differentiate between different Web clients.
Remote_host The value of this environment variable contains the host name of the client that sent the CGI request. If you do not support the query, you do not need to define this environment variable.
Request_method Provides the method that the script is called. For scripts that use the http/1.0 protocol, only get and POST make sense.
Script_filename The full path of the CGI script
Script_name The name of the CGI script
server_name This is the host name, alias, or IP address of your WEB server.
Server_software The value of this environment variable contains the name and version number of the HTTP server that called the CGI program. For example, the value above is apache/2.2.14 (Unix)
Get and Post methods

The browser client passes information to the server in two ways, both the GET method and the POST method.

Using cookies in CGI

In the HTTP protocol a big drawback is not to make the user's identity judgment, so that the programmer to bring great inconvenience,

The advent of the cookie function compensates for this shortcoming.

All cookies are written on the client's hard drive through the client's browser while the client accesses the script, and when the next client accesses the script, it retrieves the data information, which is used in the password judgment.

 

The syntax of a cookie

HTTP cookie is sent through the HTTP header to achieve, he was earlier than the file transfer, the head Set-cookie syntax as follows:

< Code class= "Python functions" >set - cookie: Name = name;expires = date;path = path;domain = domain; Secure
    • Name=name: The value of the cookie needs to be set (name cannot use ";" and ","), with multiple name values separated by ";" For example: Name1=name1;name2=name2;name3=name3.

    • Expires=date:cookie expiration date, format: expires= "wdy,dd-mon-yyyy HH:MM:SS"

    • Path=path: Sets the path supported by the cookie, if path is a path, then the cookie takes effect for all files and subdirectories in this directory, for example: Path= "/cgi-bin/", if path is a file, the cookie means that the file is in effect, For example: Path= "/cgi-bin/cookie.cgi".

    • Domain=domain: The domain name that is valid for the cookie, for example: domain= "www.chinalb.com"

    • Secure: If this flag is given, it means that the cookie can only be passed through an HTTPS server on the SSL protocol.

The receipt of a cookie is implemented by setting the environment variable Http_cookie, which can be retrieved by the CGI program to obtain cookie information.

Retrieving cookie Information

The Cookie Information retrieval page is very simple and the cookie information is stored in the CGI environment variable Http_cookie, which is stored in the following format:

key1 = value1;key2 = value2;key3 = value3....

Python chat--cgi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.