1,430   Apache Python

一,Web server 配置

以 apache 为例

# 指定 cgi 目录
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"


<Directory "/var/www/cgi-bin">
	# 添加 python 后缀
    AddHandler cgi-script .cgi .py .pl
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all

</Directory>

二,cgi 编写

 

简单的 hello.py
需要赋权执行 chmod +x hello.py

#!/usr/bin/python
#-*- encoding:utf-8 -*-
print "Content-type: text/html"
print "" # 空行,标记头部结束
print "Hello, world!"

接收 get 参数

#!/usr/bin/python
#-*- encoding:utf-8 -*-

import cgi,commands,sys

form = cgi.FieldStorage()

type=form.getvalue('type')


print "Content-type: text/html"
print "" # 空行,标记头部结束
print(type)




Leave a Reply

Your email address will not be published. Required fields are marked *