install mod_python  

一直习惯用java写程序,突然想换换口味,正好手边有apache,给它装个mod_python。

1.到www.modpython.org下载个新版 (注意版本问题apache和python版本)
2.拷到linux机器上,下面在命令行执行:
tar -zxvf mod_python-3.3.1.tgz
cd mod_python-3.3.1
./configure –with-apxs=/usr/local/apache/bin/apxs –with-python=/usr/bin/python

make
make install
3.这些编译完了,会在apache/modules/目录下生成mod_python.so,大概3M左右。
4.配置apache的http.conf
LoadModule python_module modules/mod_python.so
<Directory “/usr/modpython”> # 能用apache访问的目录
#AddHandler mod_python .py
SetHandler mod_python
PythonHandler mod_python.publisher
PythonDebug On
</Directory>
5.测试
在/usr/modpython/目录下新建一个test.py
#coding:gb2312
def index(req):
req.write(“hello,world!”)
return
6.运行,启动apache没有错误后,打开http://localhost/modpython/test
即可看到helloworld了
7.定义其他方法:
#coding:gb2312
def index(req):
req.write(“hello,world!”)
return
def hello(req):
req.write(“hello!!!”)
return
可以通过:http://localhost/modpython/test/hello来访问。
8.传递参数
def get(req,name=””):
if name:
req.write(“参数:”+name);
else:
req.write(“no param.”);
return
可以通过:http://localhost/modpython/test/hello?name=smallfish来访问。
POST表单一样,只要参数名写对就行。
9.python包
在当前目录下建立一个包,然后在test.py导入时候会出错,找不到包。后来修改了下方法
import os,sys
sys.path.append(os.path.dirname(__file__)) # 把当前目录加入到sys.path中
import 自己的包

安装结束了。

 

欢迎大佬支持本博客的发展 -- Donate --

本文链接:install mod_python

转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:三十岁,谢谢!^^


分享到:          
  1. 没有评论

  1. 没有通告