CGI

来自维基百科

In computing, Common Gateway Interface (CGI) offers a standard protocol for web servers to execute programs that execute like Console applications (also called Command-line interface programs) running on a server that generates web pages dynamically. Such programs are known as CGI scripts or simply as CGIs. The specifics of how the script is executed by the server are determined by the server.[1] A CGI script essentially reads a query string and produces a HTML page.[2]

CGI接口定义了server如何执行脚本并返回一个HTML页面.

实际上, 在网络发展早期, 浏览器向服务器发送HTTP请求, 首先会到达Http Web Software, 如IIS, Apache, Nginx, Node-js服务器等. 然后这些服务器捕获了这些HTTP请求, 会解析出请求的内容, 并将具体的请求内容调用一些脚本或者程序, 执行完之后会组成html的输出, 返回给client端. CGI就是HTTP请求通过服务器软件传给脚本的接口定义.

This is usually done by marking a directory within the document collection as containing CGI scripts - its name is often cgi-bin. For example, /usr/local/apache/htdocs/cgi-bin could be designated as a CGI directory on the web server. When a Web browser requests a URL that points to a file within the CGI directory (e.g., http://example.com/cgi-bin/printenv.pl/with/additional/path?and=a&query=string), 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.

由web server执行脚本, 但这里的脚本或者软件, 都应该是命令行的, 在web server中会有一个目录(虚拟, 总之是一个Dict结构, 映射了url和script的对应关系), 如果收到client的对文件的请求, web server就会把本来返回脚本文件的行为变为执行脚本, 然后返回脚本输出到标准输出的内容, 并返回给client.

As remarked above, the CGI standard defines how additional information passed with the request is passed to the script. For instance, if a slash and additional directory name(s) are appended to the URL immediately after the name of the script (in this example, /with/additional/path), 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; in the example, ?and=a&query=string), 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.[10]

这里还涉及到对于HTTP请求方式的不同, 上面的例子上是当请求server software的脚本文件时,

请求是

http://example.com/cgi-bin/printenv.pl/with/additional/path?and=a&query=string

如果是HTTP GET请求, 在请求脚本文件后还加了/with/additional/path, 这个会被保存在一个环境变量中PATH_INFO中, 而后面的请求参数也会被保存在一个环境变量QUERY_STRING中.

如果是HTTP POST请求, 在HTTP请求体里面的参数会被重定向到脚本的标准输入, 然后执行脚本.

FastCGI

FastCGI是CGI的一个变种, 是一个二进制协议, 目的在于减少server software和CGI pragrams的过度交互, 并使server可以一次处理多个请求. CGI Application是one request, one process, 效率比较低.

FastCGI可以认为是web server和application的中间层, FastCGI有一个一直存在的service process, 通过socket, pipe或者是TCP和web server的进程通信, 会不断把请求参数传递给application.

Python WSGI

WSGI(Web Server GateWay Interface)

在CGI和FastCGI之上, Python实现了WSGI, 作为对于使用Python开发web application时同web server的接口. WSGI中有middleware的概念, 类似于FastCGI, 也是中间层.

cgi_and_fastcgi的更多相关文章

随机推荐

  1. CCF 201512-1 数位之和 (水题)

    问题描述 给定一个十进制整数n,输出n的各位数字之和. 输入格式 输入一个整数n. 输出格式 输出一个整数,表示答案. 样例输入 20151220 样例输出 13 样例说明 20151220的各位数字 ...

  2. HDU - 1099 - Lottery - 概率dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1099 最最简单的概率dp,完全是等概率转移. 设dp[i]为已有i张票,还需要抽几次才能集齐的期望. 那么dp[ ...

  3. POJ 3067【树状数组】

    题意: 给你两行数字,n个m个,然后给你k条线直接把两个数连起来,问有多少个交叉的 思路: 假定上一行是起点,下一行是终点. 把路按照起点从大到下排序, 然后可以直接对每条路查询,这条路目前的交叉数, ...

  4. css之BFC(block formmatting context)[格式化上下文]

    一.定义: BFC就是一个黑盒子,可以保证盒子内部元素不管如何变化,都不会影响盒子附近的元素:它属于普通流.浮动.定位方案中的普通流. 二.触发条件: 1.body: 2.float(不包含none) ...

  5. WINDOWS编程基础-最简单的windows程序

    流程 1 建立并注册windows类 2 使用windows类创建窗口 3 实现事件处理,主循环 PeekMessage与GetMessage的对比 相同点: PeekMessage函数与GetMes ...

  6. 浅谈volatile关键字

    volatile是一种轻量级的同步机制.它可以保证内存可见性以及防止指令重排序,但是不保证原子性 volatile和JMM机制是不可分割的,在谈volatile的时候有必要先了解以下JMM JMM(J ...

  7. PHP不重新编译,单独添加模块扩展的方法

    php自身提供了很多扩展,比如curl,gmp, mbstring等.我们在编译安装php时未必安装了所有扩展.那么在安装完php后,如果想单独安装某个php自身的扩展怎么办呢? 我们以curl扩展模 ...

  8. STP-3-收敛到新的STP拓扑

    事实上,即使拓扑已经稳定,STP也从未停止工作,对每个收到的BPDU,交换机都会重新计算自己对于根桥,RP,DP的选择.在稳定的拓扑中,交换机收到的BPDU不变,因此对这些BPDU的处理会一遍一遍产生 ...

  9. python之序列化json模块与pickle模块(待补充)

    一.json是所有语言都通用的一种序列化格式 只支持 : 列表,字典字符串,数字,且字典的key必须是字符串 ''' 1. dumps , loads 在内存中做数据转换: dumps : 数据类型 ...

  10. python转换已转义的字符串

    python转换已转义的字符串 有时我们可能会获取得以下这样的字符串: >>> a = '{\\"name\\":\\"michael\\"} ...