In the previous article, " What can python do? Python real-World application scenario parsing", a brief introduction of
python ProgrammingThe role and benefits, along with the extension of knowledge points, we come to briefly introduce what is
Python CGI programming.
If you want to know what a Python CGI program is, you'll need to know what a CGI
CGI is currently maintained by NCSA, and NCSA defines CGI as follows:
CGI (Common Gateway Interface), a universal Gateway interface, is a program that runs on a server such as an HTTP server, providing an interface with the client HTML page.
Web browsing
To get a better idea of how CGI works, we can click on a link or URL from the Web page:
1. Use your browser to access the URL and connect to the HTTP Web server.
2. When the Web server receives the request information, it resolves the URL and finds whether the file accessed exists on the server and returns an error message if there is the contents of the returned file.
3. The browser receives the information from the server and displays the received file or error message.
CGI programs can be Python scripts, Perl scripts, shell scripts, C or C + + programs, and so on.
CGI Frame composition
first python CGI programming
Python to create the first CGI programming , the file name is hello.py, the file is located in/var/ In the Www/cgi-bin directory, the contents are as follows:
#!/usr/bin/python#-*-coding:utf-8-*-print "content-type:text/html" Print # blank line, tell the server to end the head print '
After saving the file, modify the hello.py and modify the file permissions to 755:
chmod 755 hello.py
The above programs in the browser access http://localhost/cgi-bin/hello.py display the results as follows:
Hello world! I am the first CGI program from the Chinese web of PHP
This hello.py script is a simple Python script, the output of the first line of the script "content-type:text/html" is sent to the browser and tells the browser that the content type is "text/html".
Output a blank line with print to tell the server to end the header information.