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)</span><span style="color: #660066;" class="typ">Web</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">py test</span><span style="color: #666600;" class="pun"></</span><span style="color: #000000;" class="pln">title</span><span style="color: #666600;" class="pun">></span><span style="color: #666600;" class="pun"></</span><span style="color: #000000;" class="pln">head</span><span style="color: #666600;" class="pun">></span><span style="color: #008800;" class="str"><body></span><span style="color: #000000;" class="pln">

$if greeting</span><span style="color: #666600;" class="pun">:</span><span style="color: #000000;" class="pln">
	I just wanted to say </span><span style="color: #666600;" class="pun"><</span><span style="color: #000000;" class="pln">em style</span><span style="color: #666600;" class="pun">=</span><span style="color: #008800;" class="str">"color: green; font-size: 2em;"</span><span style="color: #666600;" class="pun">></span><span style="color: #000000;" class="pln">$greeting</span><span style="color: #666600;" class="pun"></</span><span style="color: #000000;" class="pln">em</span><span style="color: #666600;" class="pun">>.</span><span style="color: #000000;" class="pln">
$else</span><span style="color: #666600;" class="pun">:</span><span style="color: #008800;" class="str"><em></span><span style="color: #660066;" class="typ">Hello</span><span style="color: #666600;" class="pun"></</span><span style="color: #000000;" class="pln">em</span><span style="color: #666600;" class="pun">>,</span><span style="color: #000000;" class="pln"> world</span><span style="color: #666600;" class="pun">!</span><span style="color: #666600;" class="pun"></</span><span style="color: #000000;" class="pln">body</span><span style="color: #666600;" class="pun">></span><span style="color: #666600;" class="pun"></</span><span style="color: #000000;" class="pln">html</span><span style="color: #666600;" class="pun">></span></pre> 
    <p style="margin-bottom: 10px;">此时,运行如如下代码:</p> 
    <pre class="prettyprint prettyprinted"><span style="color: #000000;" class="pln">python bin</span><span style="color: #666600;" class="pun">/</span><span style="color: #000000;" class="pln">app</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">py</span></pre> 
    <p style="margin-bottom: 10px;">然后访问localhost:8080/就会出现如下效果: <br>I just wanted to say <em>Hello World</em> . <br>这时,我们再执行如下命令:</p> 
    <pre class="prettyprint prettyprinted"><span style="color: #000000;" class="pln">touch templates</span><span style="color: #666600;" class="pun">/</span><span style="color: #000000;" class="pln">book</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">html
vi templates</span><span style="color: #666600;" class="pun">/</span><span style="color: #000000;" class="pln">book</span><span style="color: #666600;" class="pun">.</span><span style="color: #000000;" class="pln">html</span></pre> 
    <p style="margin-bottom: 10px;">输入如下代码:</p> 
    <pre class="prettyprint prettyprinted"><span style="color: #000088;" class="tag"><html></span><span style="color: #000088;" class="tag"><head></span><span style="color: #000088;" class="tag"><title></span><span style="color: #000000;" class="pln">BOOK</span><span style="color: #000088;" class="tag">
BOOK

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

你可能感兴趣的:(web.py)