Centos: Python CGI Script environment deployment
2016-05-24 20:47
453 查看
Introduction:
Common Gateway Interface (CGI) is a standard way for web servers to interface with executable programs installed on a server that generate web pages dynamically. Such programs are known as CGI scripts or simply CGIs; they are usually written in a scriptinglanguage, but can be written in any programming language such as python, perl, shell, c or c++ etc.
Deployment:
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 acgi-bin/directory at the base of the directory tree and treat all executable files(for example,
chmod 755 var/www/cgi-bin/**.cgi), within this directory (and no other, for security) as CGI scripts. Another popular convention is to use filename extentions; 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.
CGI architecture:
Web server configuration:
Apache: 2.4.6By default, the CGI directory alias has been set to "/var/www/cgi-bin".
Then, edit the apache configuration file "httpd.conf"(usually in the /etc directory) as follows:
pre-modified:
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
post-modified:
<Directory "/var/www/cgi-bin">
AllowOverride None
Options +ExecCGI
Require all granted
</Directory>
Last, find the line "#AddHandler cgi-script .cgi" and omit the "#", append the " .py"(Notice the whitespace) or " .pl"(if perl),
Restart the apache server to make the modification working.
Writing a Python CGI Script:
Example:(hello.py)#!/usr/bin/python # -*- coding: UTF-8 -*- print "Content-type:text/html" print # Null line,tell the server that the header is ending. print '<html>' print '<head>' print '<meta charset="utf-8">' print '<title>Hello Word, My first Python CGI Script!</title>' print '</head>' print '<body>' print '<h2>Hello Word, My first Python CGI Script!</h2>' print '</body>' print '</html>'
Precaution: the first line must be here, which specifies the python execution directory. Or, the apache may response "500 Error", "Internal Server Error".
If you have modified the default CGI directory, the script must be put in the correspondent directory.(by default, it should be in the "/var/www/cgi-bin")
Lastly, to modify the permission of the script to let the server have the right to run it.
chmod 755 hello.py
Conclusion:
Assuring your apache server has been started, then input the address "http://localhost/cgi-bin/hello.py" to make sure your job is well done.
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Seafile Server本地权限提升漏洞(CVE-2014-5443)
- 从USB安装Ubuntu Server 10.04.3 图文详解
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例