Python2.7 's cgihttpserver can be used as a simple HTTP server, capable of invoking CGI scripts
1 Create a special directory cgi-bin in any directory to hold scripts (. py or. cgi) that you write
2 Execute python-m cgihttpserver in the directory where Cgi-bin is located , start the server
3 in the browser input IP:8000/cgi-bin/xxx.py can call xxx.py script in Cgi-bin directory (Linux may want to change permissions 755)
Example 1
hello.py
#!/usr/bin/python #for Linux# -*-coding:utf-8-*-print" content-type:text/html\r\n\r\n" #HTTP的header \r\n\r\n Means the end of the head, the contents of the back are bodyprint " " "
Headers
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 |
Python CGI programming and Cgihttpserver