Common Gateway Interface Python CGI编程

时间:2022-09-23 19:01:21

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 modulesNSAPI 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:

Common Gateway Interface Python CGI编程的更多相关文章

  1. 什么是CGI&lpar;Common Gateway Interface&rpar;?

    参考: 1.Python CGI编程 2.十分钟搞懂CGI 3.CGI Made Really Easy

  2. 转:python cgi编程

    转:http://www.runoob.com/Python/python-cgi.html 什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway ...

  3. tomcat servlet JSP common gateway interface 公共网关接口

    Tomcat主要充当servlet/JSP容器,不过它却有大量的功能可以与传统的Web服务器相媲美,对公共网关接口(Common Gateway Interface)的支持就是其中之一. 传统的Web ...

  4. 吴裕雄--天生自然python学习笔记:Python CGI编程

    什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP服务器,提供同客户 ...

  5. Python CGI编程

    CGI(Common Gateway Interface)通用网关接口,它是一段程序,运行在服务器上.如:HTTP服务器,提供同客户端HTML页面的接口. CGI程序可以是python脚本,PERL脚 ...

  6. Python CGI编程&lpar;转自易百&rpar;

    Python CGI编程 Python的CGI编程,公共网关接口或CGI,Web服务器和一个自定义的脚本之间交换信息是一组定义的标准.     什么是CGI ? 公共网关接口或CGI,Web服务器和一 ...

  7. python CGI编程-----简单的本地使用(1)

    本章节需要安装python开发工具,window平台安装地址:https://www.python.org/downloads/windows/,linux安装地址:https://www.pytho ...

  8. python CGI 编程实践

    文章更新于:2020-03-05 注1:安装 python 参见: python 的安装使用和基本语法 注2:配置 web 环境参见: Windows&linux使用集成环境搭建 web 服务 ...

  9. Python CGI编程和CGIHTTPServer

    Python2.7 的CGIHTTPServer 可以作为一个简单的HTTP服务器,能够调用cgi脚本 1 在任意目录下创建一个特殊的目录 cgi-bin ,用于存放自己写的脚本(.py或.cgi) ...

随机推荐

  1. android回收AnimationDrawable动画的每一帧的图片资源,而释放内存

    回收每一帧的图片,释放内存资源 private void tryRecycleAnimationDrawable(AnimationDrawable animationDrawables) { if ...

  2. 深入理解javascript(一)

    此段文章摘自大叔的博客: 此文的目的是书写可维护的javascript代码. 最小的全局变量: JavaScript通过函数管理作用域.在函数内部声明的变量只在这个函数内部,函数外面不可用.另一方面, ...

  3. ubuntu静态IP配置

    1. 修改配置文件/etc/network/interfacesroot@ubuntu:~# sudo gedit /etc/network/interfaces 添加以下内容:auto eth0   ...

  4. TypeC一个微软开发的超简单&period;NET依赖注入&sol;IoC容器

    控制反转(IoC,Inversion of Control)是由Martin Fowler总结出来的一种设计模式,用来减少代码间的耦合.一般而言,控制反转分为依赖注入(Dependency Injec ...

  5. 试图从数据库 &OpenCurlyQuote;UFData&lowbar;001&lowbar;2003&&num;39&semi; 中提取的逻辑页 &lpar;1&colon;10720&rpar; 属于对象 &&num;39&semi;0&&num;39&semi;,而非对象 &&num;39&semi;syscolumns&&num;39&semi;

    数据库可以使用,可以备份,但对备份进行恢复时报错,使用sp_attach_db对两个物理文件进行连接时,报同样错误: 服务器: 消息 605,级别 21,状态 1,行 1 试图从数据库 ‘UFData ...

  6. PDF&sol;WORD&sol;EXCEL 图片预览

    一.PDF/WORD/EXCEL 转 XPS 转 第一页内容 转 图片 WORD.EXCEL转XPS (Office2010) public bool WordToXPS(string sourceP ...

  7. &lbrack;CF940F&rsqb;Machine Learning

    codeforces 一句话题意 求区间数字出现次数的mex,带修改 sol 带修膜队不解释 带修膜队的排序! struct query{ int id,l,r,t; bool operator &l ...

  8. james2&period;3 配置收件 之 MariaDB数据库配置

    james我们公司一直都是使用的2.3这个稳定版本,现在已经有3.0了,不过无所谓,能用就行 基于2.3,来进行一些配置,主要是接受邮件,之前的博文如何安装的,这里不多做介绍了,链接参考:https: ...

  9. QT编写TCP的问题

    ---->>>TCP编写实战的小项目 TCP套接字:主机(IP+端口) 和 服务器(IP+端口) 进行通讯,需要中间的一个锁套进行  啮合,这个锁套就是套接字的作用. 其中套接字的使 ...

  10. ASP&period;NET MVC与Sql Server交互&comma;把字典数据插入数据库

    在"ASP.NET MVC与Sql Server交互, 插入数据"中,在Controller中拼接sql语句.比如: _db.InsertData("insert int ...