web.py轻量级开发框架——python的超轻量级服务器

web.py轻量级开发框架——python的超轻量级服务器

运行环境:

操作系统:Ubuntu 12.04 STL 
python版本:2.7.5

1.安装web.py 
打开终端,执行命令:

sudo easy_install web.py

2.安装lpthw.web 
安装lpthw.web的目的纯粹是为了打印log,便于后台查看, 
依旧在终端中,运行命令:

sudo pip install lpthw.web

OK,此时开始写一个简单的程序试一下: 
执行如下命令:

mkdir karlweb
cd karlweb
mkdir bin gothonweb tests docs templates
touch bin/app.py
touch templates/index.html

此时,一个web.py的项目创建完毕,接下来开始编辑, 
执行如下代码:

vi bin/app.py

然后输入如下代码:

import web

urls =('/','Index',#url地址的映射'/book','Book')

app = web.application(urls, globals())

render = web.template.render('templates/')classIndex(object):#此类与‘/’地址映射def GET(self):
		greeting ="Hello World"return render.index(greeting = greeting)classBook(object):#此类与‘/book’地址映射def GET(self):return render.book()if __name__ =="__main__":
	app.run()

保存并退出,执行如下命令:

touch templates/index.html
vi templates/index.html

输入如下代码:

$def with(greeting)<html><head><title>Web.py test</title></head><body>

$if greeting:
	I just wanted to say <em style="color: green; font-size: 2em;">$greeting</em>.
$else:<em>Hello</em>, world!</body></html>

此时,运行如如下代码:

python bin/app.py

然后访问localhost:8080/就会出现如下效果: 
I just wanted to say Hello World . 
这时,我们再执行如下命令:

touch templates/book.html
vi templates/book.html

输入如下代码:

<html><head><title>BOOK</title></head><body><center>BOOK</center></body></html>

然后我们访问localhost:8080/book将会看到浏览器显示BOOK。 
从上面的过程可以看出webpy比较不错的集成了mvc设计模式,实现了多层架构, 
而且webpy非常轻量级,值得程序开发人员学习一下

猜你喜欢

转载自jatter.iteye.com/blog/2407677