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
相关文档:
使用 Python 分离中文与英文的混合字串
LiYanrui
posted @ 大约 1 年前
in 程序设计
with tags
python
, 614 阅读
这个问题是做 MkIV 预处理程序
时搞定的,就是把一个混合了中英文混合字串分离为英文与中文的子字串,譬如,将 ”我的 English 学的不好
&ld ......
作者:老王
Python似乎很讨厌修饰符,没有常见的static语法。其静态方法的实现大致有以下两种方法:
第一种方式(staticmethod):
>>> class Foo:
str = "I'm a static method."
def ba ......
在基于互联网的应用中,程序经常需要自动地发送电子邮件。如:一个网站的注册系统会在用户注册时发送一封邮件来确认注册;当用户忘记登陆密码的时候,通过邮件来取回密码。smtplib模块是python中smtp(简单邮件传输协议)的客户端实现。我们可以使用smtplib模块,轻松的发送电子邮件。下面的例子用了不到十行代码来发送电子邮 ......
网络臭虫亦即网络信标,是通过某种手段隐式获取信息的的方法。
在bottle framework中可用下面的方法实现:
首先在你要用户浏览器显示的页面tpl上嵌入一个1*1像素的图片,或更小。
这个图片地址指向你服务器上某个特定位置,如static/webtrack.png。
用户打开网页,就会访问这个图片(通过浏览器如http://abc.abc.com/st ......