This two days to learn the Python CGI programming, quite fun to share with you:
Goal:
Create a CGI script to receive the caller's parameters
Steps:
Create a CGI service root directory (create it, and then switch to that directory under terminal or CMD)
python2.x httpserver boot: python-m cgihttpserver 13333 will start HTTP server on port 13333
python3.x httpserver start: python-m http.server--cgi 13333 This--cgi can not be removed or the CGI script will not run
And then this time you can create a test for example index.html to visit the
Next create the Cgi-bin directory in the current directory (the directory name must be this, the CGI script is placed in this directory), at this time, relative to the HTTP service, the root directory is the previous level of the directory
can create a index.py under Cgi-bin, content:
#!/usr/bin/env pythonimport cgi #内置模块html = "
The effect will appear when you visit a two text box:
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/89/F4/wKioL1gi10jz471XAAAzQU8Undc097.png-wh_500x0-wm_3 -wmp_4-s_3850704049.png "title=" 9~hd[kf ' Y ' e$2h) n$37]6do.png "alt=" Wkiol1gi10jz471xaaazqu8undc097.png-wh_50 "/>
There are several pits: one is the starting position of a Python CGI module must be in the upper directory of the Cgi-bin;
Second, index.py must be under the Cgi-bin and insufficient authority will be error; third, when instantiating a CGI fieldstorage class, be careful not to declare multiple times, such as I do in the upper code of the try there to instantiate, similar to this:
Try:
Print (html% (CGI. fieldstorage["Name"].value,cgi. fieldstorage["Age"].value)
Except ...
The result of this writing will not show anything because the first instantiation will pass the data but the second time there is no result of the submission, so it is possible that God horse does not show or only show one;
The last thing to note is this:
Print ("content-type:text/html")
Print ()
This must be written, this is the declaration of Head and head end, do not write a problem; for the time being I have summed up so much ~
Come with me a word of English:
It always seems impossible until It do
Before success, everything seems to be impossible.
This article is from "carrying a shovel, filling all kinds of pits" blog, please be sure to keep this source http://forthedream.blog.51cto.com/11242688/1871140
A little bit of experience in learning python CGI