Python CGI程式设计

Python Programming language is broadly used by developers to create standalone, desktop, enterprise and web applications these days. Here in this article, I’m going to cover trending CGI programming in python. If you have just marked your presence in the python programming universe, then I believe you should go for Simplilearn’s Python Training accessible anytime over the internet. Here, you’ll learn about developing simple to complex python based applications with briefly described basics to advanced terms used in programming. It will also make you develop the python programming based game- flappy bird clone.

如今,开发人员广泛使用Python编程语言来创建独立的,桌面的,企业的和Web应用程序。 在本文的本文中,我将介绍python中的CGI编程趋势。 如果您刚刚标记出自己在python编程世界中的存在,那么我相信您应该随时可以通过Internet访问Simplilearn的Python培训 。 在这里,您将学习开发简单到复杂的基于python的应用程序,并简要介绍了编程中使用的高级术语的基础知识。 它还将使您开发基于python编程的游戏-软盘鸟克隆。

Python CGI程式设计 (Python CGI Programming)

CGI (Common Gateway Interface), is a group of standards for defining the data exchange among server and customized scripts. The CGI specifications are managed by NCSA.

CGI(通用网关接口)是用于定义服务器和自定义脚本之间的数据交换的一组标准。 CGI规范由NCSA管理。

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

CGI is a set of standard designed for external gateway programs. It assists them to connect through the servers such as HTTP server. The latest version available is CGI 1.1 and CGI 1.2 is under development.

CGI是为外部网关程序设计的一组标准。 它协助他们通过服务器(例如HTTP服务器)进行连接。 可用的最新版本是CGI 1.1,正在开发CGI 1.2。

网页浏览 (Web Browsing)

Allow me to explain the concept of CGI in an elaborative way. Let’s check out what happens when you click on a particular hyperlink for browsing a URL:

请允许我以详尽的方式解释CGI的概念。 让我们看看单击特定的超链接以浏览URL时会发生什么:

  • The browser connects with HTTP web server by sending a request demanding for the URL, that is, filename.

    浏览器通过发送要求URL(即文件名)的请求来连接HTTP Web服务器。
  • Web server parses the URL and searches for the name of the file. If it recognise that file, sends it back to the main client such as browser. And, if not, an error message will be indicated, saying you requested a wrong file.

    Web服务器解析URL并搜索文件名。 如果能够识别该文件,则将其发送回主客户端,例如浏览器。 并且,如果不是,则会显示一条错误消息,提示您请求了错误的文件。
  • Browser collects the response from the web server. It either displays the error message or collected file to the client.

    浏览器从Web服务器收集响应。 它要么显示错误消息,要么向客户端收集文件。

Though, it is easy to create an HTTP server so that server can directly return output to the browser made request by executing the file instead of sending file. This feature is known as Common Gateway Interface or CGI. The programs written for it are known as CGI scripts. If Python is not your primary programming language, don’t worry, you can write CGI programs in PERL, C, C++, Shell etc.

不过,创建HTTP服务器很容易,以便服务器可以通过执行文件而不是发送文件来直接将输出返回给浏览器发出的请求。 此功能称为通用网关接口或CGI。 为此编写的程序称为CGI脚本。 如果Python不是您的主要编程语言,请放心,您可以使用PERL,C,C ++,Shell等编写CGI程序。

Web服务器配置 (Web Server Configuration)

Talking more about the CGI programming always ensure that your web server supports CGI. Also, it is capable of handling CGI scripts. A pre-configured directory will be stored in the HTTP server for running all the CGI scripts. The name of this directory is CGI directory. The convention path will be /var/www/cgi-bin. CGI files support .cgi extension. Best part is you can also keep your python .py extension files in this folder.

谈论更多有关CGI编程的信息,请始终确保您的Web服务器支持CGI。 而且,它能够处理CGI脚本。 预先配置的目录将存储在HTTP服务器中,以运行所有CGI脚本。 该目录的名称是CGI目录。 约定路径为/ var / www / cgi-bin。 CGI文件支持.cgi扩展名。 最好的部分是您还可以将python .py扩展文件保留在此文件夹中。

The Linux server executes these scripts in cgi-bin directory as /var/www. For defining any specific directory to execute your script, you can make following changes in httpd.conf file:

Linux服务器在cgi-bin目录中以/ var / www执行这些脚本。 要定义任何特定目录来执行脚本,可以在httpd.conf文件中进行以下更改:

<Directory "/usr/var/www/cgi-bin">
AllowOverride None
Options ExecCGI +MultiViews
Require all granted
</Directory>
 
<Directory "/var/www/cgi-bin">
Options All
</Directory>

Now, you got a web server and you are free to execute any program written in C, Perl etc.

现在,您有了一个Web服务器,您可以自由执行用C,Perl等编写的任何程序。

编写CGI程序 (Writing CGI Program)

Let’s start by writing a simple python script which will be pointing to a CGI script known as firstpgm.py. This file is stored in the folder /var/www/cgi-bin. Now, before execution, make sure to change permission by using Unix command chmod for execution.

让我们从编写一个简单的python脚本开始,该脚本将指向一个名为firstpgm.py的CGI脚本。 该文件存储在文件夹/ var / www / cgi-bin中。 现在,在执行之前,请确保使用Unix命令chmod更改执行权限。

Chmod 755 firstpgm.py

Chmod 755第一页pgm.py

File:

文件:

#!/usr/bin/python
 
print "content-type: text/html/r/n/r/n" 
print '<html>'
print '<head>'
print '<title> Hi all- first program</title>'
print '</head>'
print '<body>'
print '<h3> Yo- successfully executed my program</h3>'
print '</body>'
print '</html>'

On execution, an output will be shown:

执行后,将显示输出:

Yo- successfully executed my program

您成功执行了我的程序

HTTP头 (HTTP Header)

In the above program the content-type: text/html\r\n\r\n represents a HTTP header. It is sent to the browser for recognising content.

在上面的程序中,内容类型:text / html \ r \ n \ r \ n表示HTTP标头。 它被发送到浏览器以识别内容。

Various HTTP-headers are: Content-type, Location: URL, Set Cookie: String, Content-Length: N, Last-Modified: Date, Expires: Date.

各种HTTP标头包括:内容类型,位置:URL,设置Cookie:字符串,内容长度:N,最后修改时间:日期,过期时间:日期。

CGI中的环境变量 (Environment Variables in CGI)

The environment variables are accessible from programs. Few environment variables broadly used in the programming are following:

可以从程序访问环境变量。 以下是在编程中广泛使用的环境变量:

  • CONTENT_TYPE: It represents the data type of the content sent by client to the server.

    CONTENT_TYPE:代表客户端发送到服务器的内容的数据类型。

  • HTTP_COOKIE: key and value pair cookie setup.

    HTTP_COOKIE:键和值对cookie设置。

  • PATH_INFO: represents the CGI script path.

    PATH_INFO:表示CGI脚本路径。

  • REMOTE_ADDR: used for authentication. It displays the IP   of the remote host.

    REMOTE_ADDR:用于身份验证。 它显示远程主机的IP。

  • REQUEST_METHOD: methods used for requests like GET and POST.

    REQUEST_METHOD:用于诸如GET和POST之类的请求的方法。

  • REMOTE_HOST: It displays the hostname.

    REMOTE_HOST:显示主机名。

  • SCRIPT_NAME: It displays the script name.

    SCRIPT_NAME:显示脚本名称。

  • SERVER_NAME: hostname of server.

    SERVER_NAME:服务器的主机名。

  • SERVER_SOFTWARE: name and version of software running over the server.

    SERVER_SOFTWARE:在服务器上运行的软件的名称和版本。

  • QUERY_STRING: It takes the URL information. This information is taken by GET method.

    QUERY_STRING:它获取URL信息。 此信息通过GET方法获取。

You can also check the list of environment variables by writing script:

您还可以通过编写脚本来检查环境变量列表:

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

Final Words

最后的话

In this article, I’ve covered the key pillar of the backend programing-CGI. I hope now you’ll able to develop your application in an effective manner. Feel free to ask any question..!!

在本文中,我介绍了后端编程CGI的关键Struts。 我希望您现在可以有效地开发应用程序。 随时问任何问题..!

翻译自: https://www.thecrazyprogrammer.com/2018/09/python-cgi-programming.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值