什么是CGI(通用网关接口)?

Web Servers are an important part of the internet and the web. Web servers used to serve web sites, web pages, APIs, documents, video images, etc. Web servers are generally designed to be dynamic to respond to user requests in a proper fashion. CGI (Common Gateway Interface) is a technology designed to provide dynamic programming and response capabilities to the web servers.

Web服务器是Internet和Web的重要组成部分。 用于服务网站,网页,API,文档,视频图像等的Web服务器。Web服务器通常设计为动态的,以适当的方式响应用户请求。 CGI(通用网关接口)是一项旨在为Web服务器提供动态编程和响应功能的技术。

什么是CGI(通用网关接口)? (What Is CGI (Common Gateway Interface)?)

CGI is the short form of the Common Gateway Interface. CGI provides the programmatic interface to the webserver where the webserver will consume the CGI scripts and applications in order to respond to the user request. It is called common because it provides a generic interface where it is used as a gateway to the different scripts and applications. CGI scripts generally produce web pages that contain HTML, JavaScript, CSS, Image, API, ASCII text, etc.

CGI是Common Gateway Interface的缩写。 CGI提供了到Web服务器的编程接口,其中Web服务器将使用CGI脚本和应用程序来响应用户请求。 之所以称为通用,是因为它提供了通用接口,在该接口中它用作通向不同脚本和应用程序的网关。 CGI脚本通常会产生包含HTML,JavaScript,CSS,图像,API,ASCII文本等的网页。

CGI is first created as a best practice in NCSA (National Center for Supercomputing Applications) in order to call command-line executables from the web. With wide usage and popularity, the standard RFC 3875 is created in November 1997.

CGI最初是作为NCSA(国家超级计算应用程序中心)中的最佳实践创建的,以便从Web调用命令行可执行文件。 标准RFC 3875具有广泛的用途和流行性,于1997年11月创建。

CGI脚本 (CGI Scripts)

CGI scripts mainly created using C programming language because of its standard uses C programming for definitions. getenv() function of the C library is used to get environment variables from the requests. CGI scripts are stored in /usr/local/apache/htdocs/cgi-bin path in general but the path can be changed according to will. CGI scripts can be used in different programming and scripting languages like Bash, Perl, Python, etc. but for easiness and practical development and change scripting languages Bash and Perl are preferred.

CGI脚本主要使用C编程语言创建,因为其标准使用C编程进行定义。 C库的getenv()函数用于从请求中获取环境变量。 CGI脚本通常存储在/usr/local/apache/htdocs/cgi-bin路径中,但是可以根据需要更改路径。 CGI脚本可以在Bash,Perl,Python等不同的编程和脚本语言中使用,但为了简便起见和实际开发以及更改脚本语言,Bash和Perl是首选。

LEARN MORE  How To Redirect Http To Https In Apache and Nginx
了解更多信息如何在Apache和Nginx中将Http重定向到Https

示例CGI脚本 (Example CGI Script)

Below we can see an example CGI script which is developed in Perl scripting language.

下面我们可以看到一个用Perl脚本语言开发的示例CGI脚本。

#!/usr/bin/perl

=head1 DESCRIPTION

printenv — This CGI script will print environment variables

=cut
print "Content-Type: text/plain\n\n";

for my $var ( sort keys %ENV ) {
    printf "%s = \"%s\"\n", $var, $ENV{$var};
}
  • `#!/usr/bin/perl` line is used to set the given file interpreter or programming language. This line will set the interpreter as Perl which is located at `/usr/bin/perl`.

    `#!/ usr / bin / perl`行用于设置给定的文件解释器或编程语言。 这行代码将解释器设置为Perl,位于/ usr / bin / perl中。
  • `=head1 DESCRIPTION` is a comment which simply put a header to the documentation and below the lines are also comment

    == head1DESCRIPTION是一个注释,它只是将标题放置在文档中,而在行下方也是注释
  • `=cut` is the end of the comment.

    == cut是注释的结尾。
  • `print “Content-Type: text/plain\n \n` will output the first line of the HTTP response for the request. This line will output the content type as plain text.

    `print'Content-Type:text / plain \ n \ n`将输出请求的HTTP响应的第一行。 此行将内容类型输出为纯文本。
  • `for my $var ( sort keys %ENV) {` block will iterate over the environment variables and put the value into `$var` and the `$var` will be printed in every step.

    对于我的$ var(排序键%ENV){`块将遍历环境变量并将值放入$ var中,并且$ var将在每个步骤中打印。

When we call this Perl CGI script we will get an output or HTTP response as HTML format like below.

当我们调用此Perl CGI脚本时,我们将获得如下HTML格式的输出或HTTP响应。

COMSPEC="C:\Windows\system32\cmd.exe"
DOCUMENT_ROOT="C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs"
GATEWAY_INTERFACE="CGI/1.1"
HOME="/home/SYSTEM"
HTTP_ACCEPT="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
HTTP_ACCEPT_CHARSET="ISO-8859-1,utf-8;q=0.7,*;q=0.7"
HTTP_ACCEPT_ENCODING="gzip, deflate, br"
HTTP_ACCEPT_LANGUAGE="en-us,en;q=0.5"
HTTP_CONNECTION="keep-alive"
HTTP_HOST="example.com"
HTTP_USER_AGENT="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:67.0) Gecko/20100101 Firefox/66.0"
PATH="/home/SYSTEM/bin:/bin:/cygdrive/c/progra~2/php:/cygdrive/c/windows/system32:..."
PATHEXT=".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC"
PATH_INFO="/foo/bar"
PATH_TRANSLATED="C:\Program Files (x86)\Apache Software Foundation\Apache2.4\htdocs\foo\bar"
QUERY_STRING="var1=value1&var2=with%20percent%20encoding"
REMOTE_ADDR="127.0.0.1"
REMOTE_PORT="63555"
REQUEST_METHOD="GET"
REQUEST_URI="/cgi-bin/printenv.pl/foo/bar?var1=value1&var2=with%20percent%20encoding"
SCRIPT_FILENAME="C:/Program Files (x86)/Apache Software Foundation/Apache2.4/cgi-bin/printenv.pl"
SCRIPT_NAME="/cgi-bin/printenv.pl"
SERVER_ADDR="127.0.0.1"
SERVER_ADMIN="(server admin's email address)"
SERVER_NAME="127.0.0.1"
SERVER_PORT="80"
SERVER_PROTOCOL="HTTP/1.1"
SERVER_SIGNATURE=""
SERVER_SOFTWARE="Apache/2.2.39 (Win32) PHP/7.1.7"
SYSTEMROOT="C:\Windows"
TERM="cygwin"
WINDIR="C:\Windows"

翻译自: https://www.poftut.com/what-is-cgi-common-gateway-interface/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值