一、軟件包需求
(1) Python-2.7.3.tgz
(2) nginx-1.2.3.tar.gz
(3) pcre-8.30.tar.gz
(4) setuptools-0.6c11.tar.gz
(5) flup-1.0.1.tar.gz
(6) web.py-0.37.tar.gz
(7) Cheetah-2.4.4.tar.gz
二、軟件包安裝
注意:./configure 選擇prefix安裝路徑,安裝nginx需要指定pcre軟件包路徑,setuptools用於python包安裝管理,安裝flup、webpy時需要使用自己安裝的python2.7
(1) python安裝
LDFLAGS="-Wl,-rpath /usr/local/xxx/opt/lib" ./configure --prefix=/usr/local/xxx/opt --enable-shared --enable-profiling --enable-ipv6 --with-gcc --with-threads
(2) nginx安裝
/configure --prefix=/usr/local/xxx/opt --with-http_stub_status_module --with-mail --with-debug --with-mail_ssl_module --with-ipv6 --with-http_ssl_module --with-pcre=../pcre-8.30
(3) setuptools安裝
/usr/local/xxx/opt/bin/python setup.py inistall
(4) flup安裝
/usr/local/xxx/opt/bin/python setup.py inistall
(5) webpy安裝
/usr/local/xxx/opt/bin/python setup.py inistall
(6) Cheetah安裝
/usr/local/xxx/opt/bin/python setup.py inistall
三、軟件配置
(1) nginx配置文件
user nobody;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream webpys {
server unix:/tmp/webpy.sock;
}
server {
listen 8008;
server_name localhost;
#charset koi8-r;
#access_log /usr/local/xxx/opt/logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
location / {
fastcgi_connect_timeout 300;
fastcgi_pass webpys;
include /usr/local/xxx/opt/conf/fastcgi_params;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
四、程序示例
程序安裝目錄:
/usr/local/xxx/app/cgi-bin
(1) 演示一個簡單的 index.py,其中使用了 middleware 中間件,用於請求連接的回調處理。
#!/usr/local/xxx/opt/bin/python
# vim: set ts=4 et sw=4 sts=4 fileencoding=utf-8 :
import web
class middleware(object):
def __init__(self, app):
object.__init__(self)
setattr(self, "innerapp", app)
def __call__(self, environ, start_response):
return self.innerapp(environ, start_response)
class test:
def GET(self):
return "Hello, World!\n"
if __name__ == "__main__":
urls = ("/.*", "test")
app = web.application(urls, globals())
web.wsgi.runwsgi = lambda func, addr=("/tmp/webpy.sock"): web.wsgi.runfcgi(func, addr)
app.run(middleware)
注意:直接運行 ./index.py 啓動程序,同時保證 /tmp/webpy.sock 權限爲777,或者和nginx爲相同權限用戶,否則nginx無權限讀取webpy.sock數據。
連接 http://localhost:8008,瀏覽器將返回 “Hello, world!“ 字符串
(2) 使用 cheetah 模板引擎,構建模板系統(直接引用 html 原始文件)
首先創建一個模板目錄,並創建模板文件 index.html,內容如下:
<html>
<title> index test template </title>
<body>
$content
</body>
</html>
其中:$content 爲模板變量
然後創建 index.py 程序,輸出模板變量內容,如下:
#!/usr/local/xxx/opt/bin/python
# vim: set ts=4 et sw=4 sts=4 fileencoding=utf-8 :
import sys
import web
from web.contrib.template import render_cheetah
# 中間件
class middleware(object):
def __init__(self, app):
object.__init__(self)
setattr(self, "innerapp", app)
def __call__(self, environ, start_response):
return self.innerapp(environ, start_response)
class index(object):
def GET(self):
# 讀取 index.html 模板
render = render_cheetah('/usr/local/xxx/app/tmpl/')
return render.index(content="hello, world!")
if __name__ == "__main__":
urls = ("/.*", "index")
app = web.application(urls, globals(), autoreload=True)
web.wsgi.runwsgi = lambda func, addr=("/tmp/webpy.sock"): web.wsgi.runfcgi(func, addr)
app.run(middleware)
(3) 使用 cheetah 模板引擎,構建模板系統(以直接引用 python 模塊的方式)
首先在模板目錄創建模板文件,命名爲 TmplIndex.tmpl,內容如下:
<html>
<title> index test template $content </title>
<body>
$content
</body>
</html>
然後執行命令,將 TmplIndex.tmpl 編譯爲 python 模塊(在相同目錄下生成同名 .py 文件):
/usr/local/xxx/opt/bin/cheetah -c /usr/local/xxx/app/tmpl/TmplIndex.tmpl
接下來創建 index.py 文件,引用 TmplIndex.py 模板 模塊,內容如下:
#!/usr/local/xxx/opt/bin/python
# vim: set ts=4 et sw=4 sts=4 fileencoding=utf-8 :
import sys
import web
# 中間件
class middleware(object):
def __init__(self, app):
object.__init__(self)
setattr(self, "innerapp", app)
def __call__(self, environ, start_response):
return self.innerapp(environ, start_response)
class index(object):
def GET(self):
# 導入模塊模塊
import TmplIndex
return TmplIndex.TmplIndex(searchList = [{"content": "hello, world"}])
if __name__ == "__main__":
urls = ("/.*", "index")
# 引入模板目錄
sys.path.append("/usr/local/xxx/app/tmpl/")
app = web.application(urls, globals(), autoreload=True)
web.wsgi.runwsgi = lambda func, addr=("/tmp/webpy.sock"): web.wsgi.runfcgi(func, addr)
app.run(middleware)