Python Web开发框架之web.py

web.py是基于Python的小巧高效的Web开发框架,而且有成熟的应用,如果感到Django复杂,这个无疑是个好的入门学习的框架。

其主页:http://webpy.org/,用的人挺多,文档很丰富。

另外最近在免费的神龙主机上弄了个空间来搞了个web.py的简单应用:一个留言版程序guestbook,来演示一下web.py的简洁和强大。

源码如下,演示了web.py的路径映射、数据库访问、模板等功能。

业务逻辑模块view.py:

#!/usr/local/python2/bin/python
#coding:utf-8
import os, sys
#导入webpy包的路径(上上层目录,根据需要调整)
sys.path.append(os.path.abspath('../../'))

#避免URL重定向时出现脚本名
os.environ['REAL_SCRIPT_NAME'] = ""

import web

#URL映射
urls = (
    '/clear/?','clear',             #清空数据库
    '/guestbook/?','guestbook',     #留言本
    '/.*', 'index',                 #首页
)

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

#模板放到当前路径templates目录
render = web.template.render('templates/')

#连接sqlite数据库,当前目录的guestbook.db
db = web.database(dbn='sqlite', db='guestbook.db')

class clear:
    def GET(self):
        #db.delete('comment')
        db.query('delete from comment')
        web.seeother('/guestbook')
       
class guestbook:
    def GET(self):
        comments=db.select('comment')
        return render.guestbook(comments)
       
    def POST(self):
        i=web.input()
        if i.author and i.content:
            db.insert('comment', author=i.author, content=i.content)
        web.seeother('/guestbook')
       
class index:
    def GET(self):
        web.seeother('/guestbook')

#fcgi运行时必需,需要flup服务器,本地运行不需要
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)

if __name__ == "__main__":
    app.run()

模板文件guestbook.html(放到当前路径的templates目录):

$def with (comments)
<html>
<body>
    <form method="post" action="/guestbook">
    <p><input type="text" name="author" />author<br />
    <input type="text" name="content" />content<br />
    <input type="submit" value="Add" /></p>
    </form>
    <p>Comments:</p>
    <ul>
    $for comment in comments:
        <li>$comment.author: $comment.content</li>
    </ul>
</body>
</html>

数据库生成脚本initDB.py(执行生成guestbook.db):

#!/usr/local/python2/bin/python
#coding:utf-8
#import sqlite3
from pysqlite2 import dbapi2 as sqlite3 #神龙主机的sqlite3库

def initDB(con):
    cur = con.cursor()
    cur.execute("""
        create table comment(
        author varchar(20),
        content varchar(1000)
        )""")
    con.commit()

if __name__ == '__main__':
    initDB(sqlite3.connect("guestbook.db"))

将这些文件上传到神龙主机,当然先要在主机上部署好web.py框架(即上传web文件夹到某目录),并设置好URL重写脚本.htaccess即可访问了,我是放在根目录的webpy目录里。

.htaccess脚本:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ webpy/view.py/$1 [QSA,L]
DirectoryIndex webpy/view.py

演示地址:http://vv0nder.freebsdhost.org/

转载请注明:来自vvonder's blog
本文地址:http://vvonderblog.appspot.com/2009/11/10/python-web-framework-webpy.html



8 条评论

我要留言
  • 1 F

    ooaixt 发表于 2009-11-12 at 19:22 回复 引用

    在神龙上申请了,可惜一直没用过
  • 2 F

    yanpeng 发表于 2009-11-13 at 10:25 回复 引用

    这个不错哈
  • 3 F

    houkai 发表于 2009-11-17 at 13:45 回复 引用

    我就不浪费神龙的主机了 看你们玩!
  • 4 F

    leo 发表于 2009-12-16 at 19:01 回复 引用

    按照你的例子做了,出现这个错误是啥原因啊

    at /guestbook
    'content'
  • 5 F

    leo 发表于 2009-12-16 at 19:01 回复 引用

    <type 'exceptions.AttributeError'> at /guestbook


    'content'

  • 6 F

    leo 发表于 2009-12-16 at 19:03 回复 引用

    提交 的时候 总是显示Post 的这行有问题
            if i.author and i.content:

  • 7 F

    leo 发表于 2009-12-16 at 19:09 回复 引用

    找到原因了,数据库设置问题哦~

    content varchar(1000)  这里1000 改成256就好了!

  • 8 F

    Coach Factory Outlet 发表于 2011-05-14 at 16:03 回复 引用

    数据库链接不上