cundi / blog

push issues as a blog
2 stars 0 forks source link

Python CGI 编程[翻译] #10

Open cundi opened 9 years ago

cundi commented 9 years ago

原始链接:http://www.tutorialspoint.com/python/python_cgi_programming.htm

Python CGI Programming

The Common Gateway Interface, or CGI, is a set of standards that define how information is exchanged between the web server and a custom script. The CGI specs are currently maintained by the NCSA and NCSA.

通用网关接口——CGI是一组定义web服务器和定制脚本之间如何交换信息的标准。CGI技术标准现在由NCSA组组维护。

What is CGI? 什么是CGI

The Common Gateway Interface, or CGI, is a standard for external gateway programs to interface with information servers such as HTTP servers.

通用网关接口CGI是一个用来扩展网关程序连接HTTP服务器这类信息服务器的标准。

The current version is CGI/1.1 and CGI/1.2 is under progress.

当前维护的版本是CGI/1.1和CGI/1.2。

Web Browsing 网页浏览

To understand the concept of CGI, let us see what happens when we click a hyper link to browse a particular web page or URL.

为了理解CGI的概念,让我们来看看当我们点击一个超链接,浏览一个网页或者URL时发生了什么事情。

Your browser contacts the HTTP web server and demands for the URL, i.e., filename.

浏览器按照你所请求的URL、文件名、等等与HTTP web服务器连接。

Web Server parses the URL and looks for the filename. If it finds that file then sends it back to the browser, otherwise sends an error message indicating that you requested a wrong file.

Web服务器解析URL并且查找文件名。如果找到该文件则发送回浏览器,否则的话就返回一个表明你所请求的文件不存在的错误消息。

Web browser takes response from web server and displays either the received file or error message.

Web浏览器从web服务器接受响应并显示接收到的内容或者显示一个错误消息。

However, it is possible to set up the HTTP server so that whenever a file in a certain directory is requested that file is not sent back; instead it is executed as a program, and whatever that program outputs is sent back for your browser to display. This function is called the Common Gateway Interface or CGI and the programs are called CGI scripts. These CGI programs can be a Python Script, PERL Script, Shell Script, C or C++ program, etc.

CGI 架构图

cgiarch

Web Server Support and Configuration Web服务器与配置

Before you proceed with CGI Programming, make sure that your Web Server supports CGI and it is configured to handle CGI Programs. All the CGI Programs to be executed by the HTTP server are kept in a pre-configured directory. This directory is called CGI Directory and by convention it is named as /var/www/cgi-bin. By convention, CGI files have extension as.cgi, but you can keep your files with python extension .py as well.

By default, the Linux server is configured to run only the scripts in the cgi-bin directory in /var/www. If you want to specify any other directory to run your CGI scripts, comment the following lines in the httpd.conf file −

<Directory "/var/www/cgi-bin">
   AllowOverride None
   Options ExecCGI
   Order allow,deny
   Allow from all
</Directory>

<Directory "/var/www/cgi-bin">
Options All
</Directory

Here, we assume that you have Web Server up and running successfully and you are able to run any other CGI program like Perl or Shell, etc.

First CGI Program 你的第一个CGI程序

Here is a simple link, which is linked to a CGI script called hello.py. This file is kept in /var/www/cgi-bin directory and it has following content. Before running your CGI program, make sure you have change mode of file using chmod 755 hello.py UNIX command to make file executable.

#!/usr/bin/python

print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>

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

Hello Word! This is my first CGI program

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

By 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 external system also to exchange information such as RDBMS.

HTTP Header HTTP头部

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.