Python Web开发与Apache2+mod_wsgi部署
最近在看Python Web方面的开发,初步接触了Django和web.py(注:和web2py完全不相关)两个框架。
如果结合Apache部署需要一些研究,可能对CGI的不熟悉吧。虽然mod_python也可以,但似乎更流行mod_wsgi
,我是在Ubuntu安装的,本来想源码编译的,可是make的时候总是报错,最后$sudo apt-get install libapache2-mod-wsgi ,倒也简单。
注:如果采用编译的方式,需要预装apxs2(apxs is the tool for building modules for Apache (apxs2 is for apache2),
$sudo apt-get install apache2-threaded-dev
装好就是修改配置文件了,主要是修改/etc/apache2/sites-available/default文件:
基本照着向导
来就可以,因为是英文的,这里简要介绍如下:
在VirtualHost内添加
#采用守护模式而非内嵌模式
WSGIDaemonProcess example.com processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup example.com
#创建python脚本执行的别名,推荐程序脚本放在独立的目录而非DocumentRoot
WSGIScriptAlias /mywebpy /usr/local/mywebpy/index.py
<Directory /usr/local/mywebpy>
Order allow,deny
Allow from all
</Directory>
web.py实例代码如下:
#!/usr/bin/env python
import web
urls = (
'/(.*)', 'hello'
)
class hello:
def GET(self, name):
i = web.input(times=1)
if not name: name = 'world'
for c in xrange(int(i.times)): print'Hello,', name+'!'
application = web.application(urls, globals()).wsgifunc()
到这里,还有一个问题,那就是代码中的print语句并不被mod_wsgi支持,你可能会看到internal error之类的信息,通过/var/log/apache2/error.og可以看到详细的信息,那是因为标准输入/输出的语句在WSGI中是不允许的,原因如下:
Q: Why do I get the error 'IOError: sys.stdout access restricted by mod_wsgi'?
A: A portable WSGI application or application component should not output anything to standard output. This is because some WSGI hosting mechanisms use standard output to communicate with the web server. If a WSGI application outputs anything to standard outpu
相关文档:
找了半天没找着,终于在英文站点上找到,还有感谢群里的石头和球迷
>>> s = datetime.datetime(2009,1,1)
>>> time.mktime(s.timetuple())
1230739200.0
别外付一个python对时间的一些函数,很好用的
我们先导入必须用到的一个module
>>> import time
设置一个时间的格式,下面会用到
& ......
作者:老王
Python似乎很讨厌修饰符,没有常见的static语法。其静态方法的实现大致有以下两种方法:
第一种方式(staticmethod):
>>> class Foo:
str = "I'm a static method."
def ba ......
self
类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称,但是
在调用这个方法的时候你不为这个参数赋值,Python会提供这个值。这个特别的变量指对象本
身,按照惯例它的名称是self。
这也意味着如果你有一个不需要参数的方法,你还是得给这个方法定义一个self参数。
__init_ ......
from link http://www.tt010.net/cms/show_article/1057.html
发表评论
Post by : BossAdmin @[2009-12-12 17:11:20] views:115
adodb:我们领导推荐的数据库连接组件
bsddb3:BerkeleyDB的连接组件
Cheetah-1.0:我比较喜欢这个版本的cheetah
cherrypy:一个WEB framework
ctypes:用来调用动态链接库
DBUtils:数 ......