教程集 www.jiaochengji.com
教程集 >  服务器技术  >  Lighttpd  >  正文 lighttpd与web.py配置web service

lighttpd与web.py配置web service

发布时间:2014-07-20   编辑:jiaochengji.com
lighttpd与web.py配置web service,lighttpd下实现webservice的教程...
lighttpd下实现webservice的教程。
搭建web service是一种灵活解决问题、分解任务的好选择。以服务的形式,以数据为驱动,实现一个庞大系统中的某一部分相对独立功能。
web service其实是一个忽略view的web server,只要关注好data就行了。而且不需要承担整个网站全部的流量任务,所以做一个轻量级的、能用的即可。lighttpd+webpy正好。
以下实验基于:gentoo linux, lighttpd-1.4.20,webpy-0.23,需要先安装lighttpd和webpy。
 
hello.py内容如下:
复制代码 代码如下:
 import web
 urls = ( '/','Index',
 )
 class Index:
 def GET(self):
 print 'hello'
 if __name__ =="__main__":
 web.run(urls,globals())
在命令行输入python hello.py 9001,在浏览器输入http://your.ip.address:9001,应该可以正常访问。
下面使用lighttpd作前台,通过fastcgi接口来调用hello.py。其实web.py就可以作为一个web server了,之所以再包装一个lighttpd是为了利用它丰富的模块与稳定的性能。毕竟lighttpd是专门做web server的,web.py只是python web server的一个轻量级实现。 lighttpd配置文件lighttpd.conf如下,可根据自己的实际需要来配置,字段名称都能顾名思义。
假设我的hello.py放在/home/wentrue/www。
复制代码 代码如下:
var.basedir = “/home/wentrue/var/www/localhost”
 var.logdir = “/home/wentrue/var/log/”
 #载入模块
 server.modules = (
 “mod_access”,
 “mod_fastcgi”,
 “mod_rewrite”,
 “mod_auth”,
 “mod_accesslog”,
 )
 
server.document-root = “/home/wentrue/www”
 server.pid-file = “/home/wentrue/var/run/lighttpd.pid”
 server.errorlog = var.logdir + “/lighttpd-error.log”
 server.port = 9001
 accesslog.filename = var.logdir + “/lighttpd-access.log”
 
#fastcgi模块设置
 fastcgi.server = ( “/hello.py” =>
 ( (
 “socket”=>”/tmp/fastcgi-wen.socket”,
 “bin-path”=>”/home/wentrue/www/hello.py”,
 “max-procs”=>2, #python子进程个数
 “check-local”=>”disable”, )))
 
#rewrite模块设置
 url.rewrite-once = (
 “^/(.*)$” => “/hello.py/$1″, #把所有请求重定向到hello.py
 )
 
#用户登录模块设置
 auth.backend = “htdigest”
 auth.backend.htdigest.userfile = server.document-root + “/htdigest.user”
 auth.require = (”/” => (
 “method” => “digest”,
 “realm” => “GreenDam”,
 “require” => “valid-user”
 ) )
需要确保自己对log目录及pid文件有写的权限。
配置访问用户名与密码,例如设置:user: GreenDam password: damnit 利用如下代码建立一个code.sh脚本:

复制代码 代码如下:
#!/bin/sh
 user=$1
 realm=$2
 pass=$3
 
hash=`echo -n “$user:$realm:$pass” | md5sum | cut -b -32`
 echo “$user:$realm:$hash”
在终端运行:sh code.sh GreenDam GreenDam damnit,得到如下字符:
GreenDam:GreenDam:ffc6bb3964cd62df5e5458924798e142
 
存放到server.document-root下一个名为htdigest.user的文件中,注意在lighttpd.conf中也要相应地设 置”realm” => “GreenDam”。
 
注意:python入口文件hello.py的权限需要设置为调用者可执行,否则fastcgi服务无法启动,如下:
 chmod 777 hello.py
 
最后lighttpd -f lighttpd.conf,启动服务。

您可能感兴趣的文章:

[关闭]
~ ~