Common Gateway Interface Python CGI编程

 

https://en.wikipedia.org/wiki/Gateway_(telecommunications)

In telecommunications, the term gateway refers to a piece of networking hardware that has the following meaning:

  • In a communications network, a network node equipped for interfacing with another network that uses different protocols.
    • A gateway may contain devices such as protocol translators, impedance matching devices, rate converters, fault isolators, or signal translators as necessary to provide system interoperability. It also requires the establishment of mutually acceptable administrative procedures between both networks.
    • A protocol translation/mapping gateway interconnects networks with different network protocol technologies by performing the required protocol conversions.
  • Loosely, a computer or computer program configured to perform the tasks of a gateway. For a specific case, see default gateway.

Gateways, also called protocol converters, can operate at any network layer. The activities of a gateway are more complex than that of the router or switch as it communicates using more than one protocol.[citation needed]

Both the computers of Internet users and the computers that serve pages to users are host nodes, while the nodes that connect the networks in between are gateways. For example, the computers that control traffic between company networks or the computers used by internet service providers (ISPs) to connect users to the internet are gateway nodes.

In the network for an enterprise, a computer server acting as a gateway node is often also acting as a proxy server and a firewall server. A gateway is often associated with both a router, which knows where to direct a given packet of data that arrives at the gateway, and a switch, which furnishes the actual path in and out of the gateway for a given packet.

On an Internet Protocol (IP) network, clients should automatically send IP packets with a destination outside a given subnet mask to a network gateway. A subnet mask defines the IP range of a private network. For example, if a private network has a base IP address of 192.168.0.0 and has a subnet mask of 255.255.255.0, then any data going to an IP address outside of 192.168.0.X will be sent to that network's gateway. While forwarding an IP packet to another network, the gateway might or might not perform Network Address Translation.

A gateway is an essential feature of most routers, although other devices (such as any PC or server) can function as a gateway.

Most computer operating systems use the terms described above. Microsoft Windows, however, describes this standard networking feature as Internet Connection Sharing, which acts as a gateway, offering a connection between the Internet and an internal network. Such a system might also act as a DHCP server. Dynamic Host Configuration Protocol (DHCP) is a protocol used by networked devices (clients) to obtain various parameters necessary for the clients to operate in an Internet Protocol (IP) network. By using this protocol, system administration workload greatly decreases, and devices can be added to the network with minimal or no manual configurations.

 

 

 

 

https://zh.wikipedia.org/wiki/网关

网关英语:Gateway),大陆译作“网关”、台湾及香港译作“闸道器”,区别于路由器(由于历史的原因,许多有关TCP/IP的文献曾经把网络层使用的路由器(英语:Router)称为网关,在今天很多局域网采用都是路由来接入网络,因此现在通常指的网关就是路由器的IP),经常在家庭中或者小型企业网络中使用,用于连接局域网Internet。 网关也经常指把一种协议转成另一种协议的设备,比如语音网关。

在传统TCP/IP术语中,网络设备只分成两种,一种为网关(gateway),另一种为主机(host)。网关能在网络间转递数据包,但主机不能转送数据包。在主机(又称终端系统,end system)中,数据包需经过TCP/IP四层协议处理,但是在网关(又称中介系统,intermediate system)只需要到达网际层(Internet layer),决定路径之后就可以转送。在当时,网关(gateway)与路由器(router)还没有区别。

在现代网络术语中,网关(gateway)与路由器(router)的定义不同。网关(gateway)能在不同协议间移动数据,而路由器(router)是在不同网络间移动数据,相当于传统所说的IP网关(IP gateway)。

网关顾名思义就是连接两个网络的设备,对于语音网关来说,他可以连接PSTN网络和以太网,这就相当于VOIP,把不同电话中的模拟信号通过网关而转换成数字信号,而且加入协议再去传输。在到了接收端的时候再通过网关还原成模拟的电话信号,最后才能在电话机上听到。

对于以太网中的网关只能转发三层以上数据包,这一点和路由是一样的。而不同的是网关中并没有路由表,他只能按照预先设定的不同网段来进行转发。网关最重要的一点就是端口映射,子网内用户在外网看来只是外网的IP地址对应着不同的端口,这样看来就会保护子网内的用户。

 

 

 

https://en.wikipedia.org/wiki/Common_Gateway_Interface

https://zh.wikipedia.org/wiki/通用网关接口

Each Web server runs HTTP server software, which responds to requests from Web browsers. Generally, the HTTP server has a directory (folder), which is designated as a document collection — files that can be sent to Web browsers connected to this server.[6] For example, if the Web server has the domain nameexample.com, and its document collection is stored at /usr/local/apache/htdocs in the local file system, then the Web server will respond to a request forhttp://example.com/index.html by sending to the browser the (pre-written) file /usr/local/apache/htdocs/index.html.

CGI extends this system by allowing the owner of the Web server to designate a directory within the document collection as containing executable scripts (or binary files) instead of pre-written pages; this is known as a CGI directory. For example, /usr/local/apache/htdocs/cgi-bin could be designated as a CGI directory on the Web server. If a Web browser requests a URL that points to a file within the CGI directory (e.g., http://example.com/cgi-bin/printenv.pl), then, instead of simply sending that file (/usr/local/apache/htdocs/cgi-bin/printenv.pl) to the Web browser, the HTTP server runs the specified script and passes the output of the script to the Web browser. That is, anything that the script sends to standard output is passed to the Web client instead of being shown on-screen in a terminal window.

The CGI system also allows the Web browser to send information to the script via the URL or an HTTP POST request. If a slash and additional directory name(s) are appended to the URL immediately after the name of the script, then that path is stored in the PATH_INFO environment variable before the script is called. If parameters are sent to the script via an HTTP GET request (a question mark appended to the URL, followed by param=value pairs), then those parameters are stored in the QUERY_STRING environment variable before the script is called. If parameters are sent to the script via an HTTP POST request, they are passed to the script's standard input. The script can then read these environment variables or data from standard input and adapt to the Web browser's request.[7]

实现维基百科编辑的CGI程序的一个例子:首先用户代理程序向这个CGI程序请求某个名称的条目,如果该条目页面存在,CGI程序就会去获取那个条目页面的原始数据,然后把它转换成HTML并把结果输出给浏览器;如果该条目页面不存在,CGI程序则会提示用户新建一个页面。所有维基操作都是通过这个CGI程序来处理的。

CGI的工作方式,从Web服务器的角度看,是在特定的位置(比如:http://www.example.com/wiki.cgi)定义了可以运行CGI程序。当收到一个匹配URL的请求,相应的程序就会被调用,并将客户端发送的数据作为输入。程序的输出会由Web服务器收集,并加上合适的档头,再发送回客户端。

一般每次的CGI请求都需要新生成一个程序的副本来运行,这样大的工作量会很快将服务器压垮,因此一些更有效的技术像mod_perl,可以让脚本解释器直接作为模块集成在Web服务器(例如:Apache)中,这样就能避免重复载入和初始化解释器。不过这只是就那些需要解释器的高级语言(即解释语言)而言的,使用诸如C一类的编译语言则可以避免这种额外负荷。由于C及其他编译语言的程序与解释语言程序相比,前者的运行速度更快、对操作系统的负荷更小,使用编译语言程序是可能达到更高执行效率的,然而因为开发效率等原因,在目前直译性语言还是最合适的。

 

如果代码只是偶尔改变的话,我们可以在服务器产生一个新的进程在编译代码之前进行处理。有一个例子是FastCGI,当然还包括其它编写的加速器,它会在第一次调用脚本时,在系统的某个地方保存脚本编译过的版本。这样对这个文件以后的请求就会自动转向这个编译过的代码,而不用每次调用脚本解释器来解释脚本。当更改了脚本,加速器的临时缓存会被清空来保证调用的是新的版本的脚本。

另一个方法是直接把解释器放在Web服务器中,这样就无须新建一个进程来执行脚本。Apache服务器有很多这样的模块,像mod_cplusplusmod_perlmod_phpmod_pythonmod_ruby、和mod_mono

 

Deployment[edit]

A Web server that supports CGI can be configured to interpret a URL that it serves as a reference to a CGI script. A common convention is to have a cgi-bin/directory at the base of the directory tree and treat all executable files within this directory (and no other, for security) as CGI scripts. Another popular convention is to use filename extensions; for instance, if CGI scripts are consistently given the extension .cgi, the web server can be configured to interpret all such files as CGI scripts. While convenient, and required by many prepackaged scripts, it opens the server to attack if a remote user can upload executable code with the proper extension.[citation needed]

In the case of HTTP PUT or POSTs, the user-submitted data are provided to the program via the standard input. The Web server creates a subset of theenvironment variables passed to it and adds details pertinent to the HTTP environment.

Uses[edit]

CGI is often used to process inputs information from the user and produce the appropriate output. An example of a CGI program is one implementing a Wiki. The user agent requests the name of an entry; the Web server executes the CGI; the CGI program retrieves the source of that entry's page (if one exists), transforms it into HTML, and prints the result. The web server receives the input from the CGI and transmits it to the user agent. If the "Edit this page" link is clicked, the CGI populates an HTML textarea or other editing control with the page's contents, and saves it back to the server when the user submits the form in it.

Alternatives[edit]

Calling a command generally means the invocation of a newly created process on the server. Starting the process can consume much more time and memory than the actual work of generating the output, especially when the program still needs to be interpreted or compiled. If the command is called often, the resulting workload can quickly overwhelm the server.

The overhead involved in process creation can be reduced by techniques such as FastCGI that "prefork" interpreter processes, or by running the application code entirely within the web server, using extension modules such as mod_perl or mod_php. The overhead can be reduced further by using compiled CGI programs, such as those in C/C++, rather than using Perl or other interpreted languages, and possibly embedding the result in the web server as a specialist module.

Several approaches can be adopted for remedying this:

  • The popular Web servers developed their own extension mechanisms that allows third-party software to run inside the web server itself, such as Apache modules, NSAPI plugins and ISAPI plugins.
  • Simple Common Gateway Interface or SCGI
  • FastCGI allows a single, long-running process to handle more than one user request while keeping close to the CGI programming model, retaining the simplicity while eliminating the overhead of creating a new process for each request. Unlike converting an application to a web server plug-in, FastCGI applications remain independent of the web server.
  • Replacement of the architecture for dynamic websites can also be used. This is the approach taken by Java EE, which runs Java code in a Java servlet container in order to serve dynamic content and optionally static content. This approach replaces the overhead of generating and destroying processes with the much lower overhead of generating and destroying threads, and also exposes the programmer to the library that comes with Java Platform, Standard Edition on which the version of Java EE in use is based.

The optimal configuration for any Web application depends on application-specific details, amount of traffic, and complexity of the transaction; these tradeoffs need to be analyzed to determine the best implementation for a given task and time budget.

 

 

 

CGI程序可以是Python脚本,PERL脚本,SHELL脚本,C或者C++程序等。

 

 

 

http://www.jdon.com/idea/cgi.htm

CGI脚本是什么?

  CGI脚本简单地讲是个运行在Web服务器上的程序, 有浏览器的输入触发. 这个脚本通常象服务器和系统中其他程序如数据库的桥梁。

  CGI 脚本难道不是一个真正的脚本?按照你的服务器的支持, 他们可能是一个编译好的程序或者批命令文件或者其他可执行的东西. 为了简单起见,我们统称他们为脚本scripts. 

  CGI 脚本是任何运行在web服务器上的程序. CGI意思是Common Gateway Interface。

  CGI脚本是用下列两种方法使用的: 作为一个表单的ACTION 或 作为一个页中的直接link。

 

 

CGI脚本是怎样工作的?

  CGI脚本由服务器调用, 基于浏览器的数据输入. 其工作原理如下:

  1. 一个URL指向一个CGI脚本. 一个CGI脚本的URL能如普通的URL一样出现,区别于.htm/.html静态UR,CGI的URL是动态URL。如http://xxxx.com/cgiurl
  2. 服务器CGI接收浏览器的请求, 按照那个URL指向对应的脚本文件(注意文件的位置和扩展名),执行CGI脚本.
  3. CGI脚本执行基于输入数据的操作,包括查询数据库、计算数值或调用系统中其他程序.
  4. CGI脚本产生某种Web服务器能理解的输出结果.
  5. 服务器接收来自脚本的输出并且把它传回浏览器,让用户了解处理结果。

 

 

https://docs.python.org/3/library/cgi.html

A CGI script is invoked by an HTTP server, usually to process user input submitted through an HTML <FORM> or <ISINDEX> element.

Most often, CGI scripts live in the server’s special cgi-bin directory. The HTTP server places all sorts of information about the request (such as the client’s hostname, the requested URL, the query string, and lots of other goodies) in the script’s shell environment, executes the script, and sends the script’s output back to the client.

The script’s input is connected to the client too, and sometimes the form data is read this way; at other times the form data is passed via the “query string” part of the URL. This module is intended to take care of the different cases and provide a simpler interface to the Python script. It also provides a number of utilities that help in debugging scripts, and the latest addition is support for file uploads from a form (if your browser supports it).

The output of a CGI script should consist of two sections, separated by a blank line. The first section contains a number of headers, telling the client what kind of data is following. Python code to generate a minimal header section looks like this:

 

posted @ 2016-09-19 22:02  papering  阅读(354)  评论(0编辑  收藏  举报