Python - CGI编程

通用网关接口(CGI)是一组标准,用于定义如何在Web服务器和自定义脚本之间交换信息. CGI规范目前由NCSA维护.

什么是CGI?

  • 公共网关接口(CGI)是外部网关程序与HTTP服务器等信息服务器接口的标准.

  • 当前版本为CGI/1.1和CGI/1.2正在进行中.

网页浏览

要理解CGI,让我们看看当我们点击超链接浏览特定网页或网址时会发生什么.

  • 您的浏览器联系HTTP Web服务器并要求提供URL,即文件名.

  • Web服务器解析URL并查找文件名.如果它发现该文件然后将其发送回浏览器,否则会发送一条错误消息,指出您请求了错误的文件.

  • Web浏览器从Web获取响应服务器并显示收到的文件或错误消息.

但是,可以设置HTTP服务器以便每当文件时在某个目录中请求不发回文件;相反,它作为一个程序执行,无论该程序输出什么,都会被发回给您的浏览器进行显示.此功能称为公共网关接口或CGI,程序称为CGI脚本.这些CGI程序可以是Python脚本,PERL脚本,Shell脚本,C或C ++程序等.

CGI架构图

Web服务器支持和配置

在继续进行CGI编程之前,请先制作确保您的Web服务器支持CGI,并且它配置为处理CGI程序. HTTP服务器要执行的所有CGI程序都保存在预先配置的目录中.此目录称为CGI目录,按照惯例,它名为/var/www/cgi-bin.按照惯例,CGI文件的扩展名为. cgi,但你也可以保存你的文件扩展名为 .py .

默认情况下,Linux服务器配置为只运行/var/www中cgi-bin目录中的脚本.如果要指定任何其他目录来运行CGI脚本,请在httpd.conf文件中注释以下行 :

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

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

在这里,我们假设您已成功运行Web Server,并且您可以运行任何其他CGI程序,如Perl或Shell等.

第一个CGI程序

这是一个简单的链接,链接到名为<a href ="https://img01.yuandaxia.cn/Content/img/tutorials/python/"的CGI脚本hello.py"target ="_ blank"> hello.py .此文件保存在/var/www/cgi-bin目录中,并具有以下内容.在运行CGI程序之前,请确保使用 chmod 755 hello.py UNIX命令更改文件模式以使文件可执行.

#!/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>'

如果单击hello.py,则会产生以下输出 :

Hello Word!这是我的第一个CGI程序

这个hello.py脚本是一个简单的Python脚本,将其输出写入STDOUT文件,即屏幕.有一个重要的额外功能可用于打印第一行内容类型:text/html \\\\\\\ .此行被发送回浏览器,它指定要在浏览器屏幕上显示的内容类型.

现在你必须已经理解了CGI的基本概念,你可以编写许多复杂的CGI使用Python的程序.此脚本还可以与任何其他外部系统交互以交换信息,例如RDBMS.

HTTP标头

Content-type:text/html\r\n\r\n是HTTP标头的一部分,发送到浏览器以了解内容.所有HTTP标头将采用以下形式 :
HTTP Field Name: Field Content

For Example
Content-type: text/html\r\n\r\n

还有其他一些重要的HTTP标头,你可以将在CGI编程中经常使用.

Sr.No.Header&说明
1

内容类型:

定义要返回的文件格式的MIME字符串.示例是Content-type:text/html

2

过期日期

信息失效的日期.浏览器使用它来决定何时需要刷新页面.有效日期字符串的格式为01 Jan 1998 12:00:00 GMT.

3

位置:网址

返回的网址而不是请求的网址.您可以使用此字段将请求重定向到任何文件.

4

最后修改日期:日期

上次修改资源的日期.

5

内容 - length:N

返回数据的长度(以字节为单位).浏览器使用此值来报告文件的估计下载时间.

6

Set-Cookie:String

设置通过字符串传递的cookie

CGI环境变量

所有CGI程序可以访问以下环境变量.这些变量在编写任何CGI程序时都起着重要作用.

Sr.No.变量名称&说明
1

CONTENT_TYPE

内容的数据类型.客户端将附加内容发送到服务器时使用.例如,文件上传.

2

CONTENT_LENGTH

查询信息的长度.它仅适用于POST请求.

3

HTTP_COOKIE

以密钥和密钥形式返回设置的cookie价值对.

4

HTTP_USER_AGENT

User-Agent请求标头字段包含有关发起请求的用户代理的信息.它是Web浏览器的名称.

5

PATH_INFO

CGI脚本的路径.

6

QUERY_STRING

使用GET方法请求发送的URL编码信息.

7

REMOTE_ADDR

发出请求的远程主机的IP地址.这是有用的记录或用于身份验证.

8

REMOTE_HOST

发出请求的主机的完全限定名称.如果此信息不可用,则可以使用REMOTE_ADDR获取IR地址.

9

REQUEST_METHOD

用于发出请求的方法.最常见的方法是GET和POST.

10

SCRIPT_FILENAME

CGI脚本的完整路径.

11

SCRIPT_NAME

CGI脚本的名称.

12

SERVER_NAME

服务器的主机名或IP地址

13

SERVER_SOFTWARE

服务器运行的软件的名称和版本.

这里是一个小的CGI程序,列出所有的CGI变量.点击此链接查看结果获取环境

#!/usr/bin/python

import os

print "Content-type: text/html\r\n\r\n";
print "<font size=+1>Environment</font><\br>";
for param in os.environ.keys():
   print "<b>%20s</b>: %s<\br>" % (param, os.environ[param])

GET和POST方法

你一定遇到过很多您需要将一些信息从浏览器传递到Web服务器并最终传递到CGI程序的情况.最常见的是,浏览器使用两种方法将这些信息传递给Web服务器.这些方法是GET方法和POST方法.

使用GET方法传递信息

GET方法发送附加到页面请求的编码用户信息.页面和编码信息由?分隔.字符如下 :

http://www.test.comhttps://www.tutorialspoint.com/cgi-bin/hello.py?key1=value1&key2=value2

GET方法是将信息从浏览器传递到Web服务器的默认方法,它会生成一个出现的长字符串在浏览器的位置:框中.如果您要将密码或其他敏感信息传递给服务器,请勿使用GET方法. GET方法有大小限制:在请求字符串中只能发送1024个字符. GET方法使用QUERY_STRING标头发送信息,并且可以通过QUERY_STRING环境变量在CGI程序中访问.

您可以通过简单地连接键和值对以及任何URL来传递信息,或者您可以使用HTML< FORM>使用GET方法传递信息的标签.

简单URL示例:获取方法

这是一个简单的URL,它将两个值传递给hello_get.py程序使用GET方法.

https://www.tutorialspoint.com/cgi-bin/hello_get.py?first_name=ZARA&last_name=ALI

下面是 hello_get.py 脚本,用于处理Web浏览器提供的输入.我们将使用 cgi 模块,这使得访问传递信息非常容易&减去;

#!/usr/bin/python

# Import modules for CGI handling 
import cgi, cgitb 

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
first_name = form.getvalue('first_name')
last_name  = form.getvalue('last_name')

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>"
print "</html>"

这将产生以下结果 :

你好ZARA ALI

简单表格示例:GET方法

此示例使用HTML FORM和提交按钮传递两个值.我们使用相同的CGI脚本hello_get.py来处理此输入.

<form action = "/cgi-bin/hello_get.py" method = "get">
First Name: <input type = "text" name = "first_name">  <br />

Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" />
</form>

以下是上述表格的实际输出,您输入名字和姓氏,然后点击提交按钮查看结果.

使用POST方法传递信息

将信息传递给CGI程序的一种更可靠的方法是POST方法.这以与GET方法完全相同的方式打包信息,而不是在a之后将其作为文本字符串发送?在URL中,它将其作为单独的消息发送.此消息以标准输入的形式进入CGI脚本.

下面是同样的hello_get.py脚本,它处理GET和POST方法.

#!/usr/bin/python

# Import modules for CGI handling 
import cgi, cgitb 

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
first_name = form.getvalue('first_name')
last_name  = form.getvalue('last_name')

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>"
print "</html>"

让我们再次采用与上面相同的示例,它使用HTML FORM和提交按钮传递两个值.我们使用相同的CGI脚本hello_get.py来处理此输入.

<form action = "/cgi-bin/hello_get.py" method = "post">
First Name: <input type = "text" name = "first_name"><br />
Last Name: <input type = "text" name = "last_name" />

<input type = "submit" value = "Submit" />
</form>

以下是上述表格的实际输出.您输入名字和姓氏,然后单击提交按钮以查看结果.

将复选框数据传递给CGI程序

复选框是当需要选择多个选项时使用.

以下是带有两个复选框和减号的表单的示例HTML代码;

<form action = "/cgi-bin/checkbox.cgi" method = "POST" target = "_blank">
<input type = "checkbox" name = "maths" value = "on" /> Maths
<input type = "checkbox" name = "physics" value = "on" /> Physics
<input type = "submit" value = "Select Subject" />
</form>

此代码的结果如下形式 :

下面是checkbox.cgi脚本处理网页浏览器为复选框按钮提供的输入.

#!/usr/bin/python

# Import modules for CGI handling 
import cgi, cgitb 

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
if form.getvalue('maths'):
   math_flag = "ON"
else:
   math_flag = "OFF"

if form.getvalue('physics'):
   physics_flag = "ON"
else:
   physics_flag = "OFF"

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Checkbox - Third CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> CheckBox Maths is : %s</h2>" % math_flag
print "<h2> CheckBox Physics is : %s</h2>" % physics_flag
print "</body>"
print "</html>"

将单选按钮数据传递给CGI程序

当只需要选择一个选项时,使用单选按钮.

以下是带有两个单选按钮的表单的示例HTML代码 :

<form action = "/cgi-bin/checkbox.cgi" method = "POST" target = "_blank">
<input type = "checkbox" name = "maths" value = "on" /> Maths
<input type = "checkbox" name = "physics" value = "on" /> Physics
<input type = "submit" value = "Select Subject" />
</form>

此代码的结果如下形式 :

以下是radiobutton.py脚本处理网页浏览器为单选按钮提供的输入 :

#!/usr/bin/python

# Import modules for CGI handling 
import cgi, cgitb 

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
if form.getvalue('subject'):
   subject = form.getvalue('subject')
else:
   subject = "Not set"

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Radio - Fourth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Selected Subject is %s</h2>" % subject
print "</body>"
print "</html>"

将文本区域数据传递给CGI程序

当多行文本必须传递给CGI时使用TEXTAREA元素程序.

以下是带有TEXTAREA框和减号的表单的示例HTML代码;

<form action = "/cgi-bin/textarea.py" method = "post" target = "_blank">
<textarea name = "textcontent" cols = "40" rows = "4">
Type your text here...
</textarea>
<input type = "submit" value = "Submit" />
</form>

此代码的结果如下形式 :

下面是textarea.cgi脚本处理web浏览器给出的输入 :

#!/usr/bin/python

# Import modules for CGI handling 
import cgi, cgitb 

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
if form.getvalue('textcontent'):
   text_content = form.getvalue('textcontent')
else:
   text_content = "Not entered"

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>";
print "<title>Text Area - Fifth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Entered Text Content is %s</h2>" % text_content
print "</body>"

将下拉框数据传递给CGI程序

如果我们有多个选项可用,则使用下拉框将选择一个或两个.

以下是表格的示例HTML代码,其中包含一个下拉框和减号;

<form action = "/cgi-bin/dropdown.py" method = "post" target = "_blank">
<select name = "dropdown">
<option value = "Maths" selected>Maths</option>
<option value = "Physics">Physics</option>
</select>
<input type = "submit" value = "Submit"/>
</form>

此代码的结果为以下形式 :

以下是dropdown.py脚本处理Web浏览器给出的输入.

#!/usr/bin/python

# Import modules for CGI handling 
import cgi, cgitb 

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
if form.getvalue('dropdown'):
   subject = form.getvalue('dropdown')
else:
   subject = "Not entered"

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Dropdown Box - Sixth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Selected Subject is %s</h2>" % subject
print "</body>"
print "</html>"

在CGI中使用Cookie

HTTP协议是无状态协议.对于商业网站,需要在不同页面之间维护会话信息.例如,一个用户注册在完成许多页面后结束.如何在所有网页上维护用户的会话信息?

在许多情况下,使用cookie是记住和跟踪偏好,购买,佣金和其他更好的信息的最有效方法访问者体验或网站统计信息.

工作原理?

您的服务器以cookie的形式向访问者的浏览器发送一些数据.浏览器可以接受cookie.如果是,则将其作为纯文本记录存储在访问者的硬盘上.现在,当访问者到达您网站上的另一个页面时,该Cookie可供检索.检索后,您的服务器知道/记住存储的内容.

Cookie是5个可变长度字段的纯文本数据记录 :

  • 过期 :  Cookie过期的日期.如果这是空白,则当访问者退出浏览器时,Cookie将过期.

  • : 您网站的域名.

  • 路径 : 设置cookie的目录或网页的路径.如果您想从任何目录或页面检索cookie,这可能是空白.

  • 安全 : 如果此字段包含单词"secure",则只能使用安全服务器检索cookie.如果此字段为空,则不存在此类限制.

  • 名称=值 :  Cookie以密钥和值对的形式设置和检索.

设置Cookie

向浏览器发送cookie非常容易.这些cookie与HTTP标头一起发送到Content-type字段.假设您要将UserID和密码设置为cookie.设置cookie的方法如下:<

#!/usr/bin/python

print "Set-Cookie:UserID = XYZ;\r\n"
print "Set-Cookie:Password = XYZ123;\r\n"
print "Set-Cookie:Expires = Tuesday, 31-Dec-2007 23:12:40 GMT";\r\n"
print "Set-Cookie:Domain = www.IT屋.com;\r\n"
print "Set-Cookie:Path = /perl;\n"
print "Content-type:text/html\r\n\r\n"
...........Rest of the HTML Content....

从这个例子中,你必须了解如何设置cookie.我们使用设置-Cookie 用于设置Cookie的HTTP标头.

设置Cookie属性(如Expires,Domain和Path)是可选的.值得注意的是,在发送魔术行之前设置了Cookie "内容类型:text/html\r\n\r\n .

检索Cookie

检索所有设置的cookie非常容易. Cookie存储在CGI环境变量HTTP_COOKIE中,它们将具有以下形式 :

key1 = value1; key2 = value2; key3 = value3 .. ..

以下是如何检索cookie的示例.

#!/usr/bin/python

# Import modules for CGI handling 
from os import environ
import cgi, cgitb

if environ.has_key('HTTP_COOKIE'):
   for cookie in map(strip, split(environ['HTTP_COOKIE'], ';')):
      (key, value ) = split(cookie, '=');
      if key == "UserID":
         user_id = value

      if key == "Password":
         password = value

print "User ID  = %s" % user_id
print "Password = %s" % password

这会产生以下cookie集结果通过以上脚本 :

User ID = XYZ
Password = XYZ123

文件上传示例

要上传文件,HTML表单必须将enctype属性设置为 multipart/form-data .带有文件类型的输入标签会创建一个"浏览"按钮.

<html>
<body>
   <form enctype = "multipart/form-data" 
                     action = "save_file.py" method = "post">
   <p>File: <input type = "file" name = "filename" /></p>
   <p><input type = "submit" value = "Upload" /></p>
   </form>
</body>
</html>

此代码的结果为以下形式 :

以上示例已被故意禁用保存人们在我们的服务器上上传文件,但你可以尝试使用你的服务器上面的代码.

这是脚本 save_file.py 来处理文件上传和减去;

#!/usr/bin/python

import cgi, os
import cgitb; cgitb.enable()

form = cgi.FieldStorage()

# Get filename here.
fileitem = form['filename']

# Test if the file was uploaded
if fileitem.filename:
   # strip leading path from file name to avoid 
   # directory traversal attacks
   fn = os.path.basename(fileitem.filename)
   open('/tmp/' + fn, 'wb').write(fileitem.file.read())

   message = 'The file "' + fn + '" was uploaded successfully'
   
else:
   message = 'No file was uploaded'
   
print """\
Content-Type: text/html\n
<html>
<body>
   <p>%s</p>
</body>
</html>
""" % (message,)

如果您在Unix/Linux上运行上述脚本,那么您需要按照以下方式更换文件分隔符,否则在您的Windows机器上面open()语句应该可以正常工作.

fn = os.path.basename(fileitem.filename.replace("\, "/" ))

如何提高"文件下载"对话框?

有时,您希望提供用户可以单击链接的选项,它将弹出"文件"将"对话框"下载到用户而不是显示实际内容.这非常简单,可以通过HTTP标头实现.此HTTP标头与上一节中提到的标头不同.

例如,如果您想从给定链接下载 FileName 文件,那么语法如下 :

#!/usr/bin/python

# HTTP Header
print "Content-Type:application/octet-stream; name = FileName\r\n";
print "Content-Disposition: attachment; filename = FileName\r\n\n";

# Actual File Content will go here.
fo = open("foo.txt", "rb")

str = fo.read();
print str

# Close opend file
fo.close()

希望你喜欢这个教程.如果是,请将您的反馈发送给我:与我们联系