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
相关文档:
先将上面创建好的testdemo工程目录\,将C:\Python25\Lib\site-packages\django\bin中的testdemo目录拷贝到自己的工作目录中,然后启动eclipse,点击“File”->“New”->“project…”,将会看到以下画面
选择“Pydev Project”,点击“Next&rdquo ......
使用pdb调试Python程序
本文讨论在没有方便的IDE工具可用的情况下,使用pdb调试python程序
源码例子
例如,有模拟税收计算的程序:
#!/usr/bin/python
def debug_demo(val):
if val &l ......
from http://stackoverflow.com/questions/101268/hidden-features-of-python
http://www.okpython.com/bbs/thread-3434-1-1.html
http://www.okpython.com/bbs/thread-3436-1-2.html
http://www.okpython.com/bbs/thread-3437-1-2.html
http://www.okpython.com/bbs/thread-3438-1-1.html
http://www.okpython.com/bb ......
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:数 ......