apache python web配置

Web服务器配置

下面配置以wampserver版的apache为例,其他版本应该大同小异的。

1.找到apache配置文件httpd.conf ,去掉下面代码前面的#号

LoadModule cgi_module modules/mod_cgi.so

2.找到

#
# "d:/wamp/bin/apache/apache2.4.9/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#

下面的类似这样的一段代码

<Directory "d:/wamp/bin/apache/apache2.4.9/cgi-bin">
    AllowOverride None
    Options None
    Require all granted</Directory>

改为:

<Directory "E:/test/python/cgi/">
    AllowOverride None
    Options Indexes FollowSymLinks ExecCGI
    Require all granted 
    Require host ip</Directory>

E:/test/python/cgi/ 这个是你的.py文件存放目录 设置CGI的访问权限和路径

3.找到:

类似:

ScriptAlias /cgi-bin/ "d:/wamp/bin/apache/apache2.4.9/cgi-bin/"

改为:

ScriptAlias /cgi-bin/ "E:/test/python/cgi/"

E:/test/python/cgi/ 这个是你的.py文件存放目录

4.找到

#AddHandler cgi-script .cgi

替换为:

AddHandler cgi-script .cgi .py

这样就可以支持python的.py文件,如果你需要解释shell的脚本文件,可以添加.pl

到此为止基本配置成功了web服务器了.

前面4行非常必要,否则可能报错

#!D:\Program Files\python27\python.exe  这个是指名python解释器(linux下的类似 #!/usr/bin/env python) # -*- coding: utf-8 -*- 指定页面编码 print("Content-type:text/html\n") 不然apache无法运行

简单的url实例:GET方法

以下是一个简单的URL,使用GET方法向hello_get.py程序发送两个参数:

/cgi-bin/test.py?name=菜鸟教程&url=http://www.runoob.com

以下为hello_get.py文件的代码:

#!/usr/bin/python
# -\*- coding: UTF-8 -\*-
# filename:test.py
# CGI处理模块
import cgi, cgitb 

# 创建 FieldStorage 的实例化
form = cgi.FieldStorage() 
# 获取数据

site\_name = form.getvalue('name')
site\_url  = form.getvalue('url')

print "Content-type:text/html"
print
print "<html>"
print "<head>"
print "<meta charset=\\"utf-8\\">"
print "<title>菜鸟教程 CGI 测试实例</title>"
print "</head>"
print "<body>"
print "<h2>%s官网:%s</h2>" % (site\_name, site\_url)
print "</body>"
print "</html>"

转载自:**https://www.cnblogs.com/zhidong123/p/6491938.html?utm_source=itdadao&utm_medium=referral

http://www.runoob.com/python/python-cgi.html

王加文博客
请先登录后发表评论
  • latest comments
  • 总共0条评论