web.py框架之基本应用

一、基本应用

1.1 Hello World!

# coding:utf-8

import web

urls = (
    "/.*", "Tk_online"
)

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

class Tk_online(object):
    
    def GET(self):
        return "Hello World!"

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

 1.2 提供静态文件

在当前应用的目录下创建static目录,将需要提供访问的静态文件放里面即可,比如JS,CSS等静态文件。

直接访问http://localhost/static/ 默认返回static里面的index.py文件

直接访问http://localhost/static/logo.png 将访问到logo.png这个文件

1.3 URL控制

web.py的URL控制模式是简单的、强大的、灵活的。在每个应用的最顶部,你通常看到整个URL调度模式被定义到元祖中:

urls = (
    "/online/?", "online",
    "/log", "log",
    "/timer", "timer"
)

你可以捕捉URL的参数,然后用在处理类中:

"/users/list/(.+)", "list_users"

在list/后面这块儿会被捕捉然后作为参数用在GET或POST:

class list_user(object):
    
    def GET(self):
        return "Listing info about user: {0}".format(name)

同样,后端获取前端的参数也可以用web.input()获取。
开发子应用的时候需要注意:

为了更好的控制大型web应用,web.py支持子应用。在为子应用设计URL模式的时候,记住去的路经是父应用剥离后的。

比如:你在主应用定义了URL "/blog"跳转到"blog"子应用,那么你在子应用中的所有URL都是以"/"开头的,而不是"/blog"。

完整应用路径如下:

webdemo
    blog
        blog.py
    __init__.py
    Main.py
    

Main.py文件

import web
from blog import blog

urls = (
    "/blog", blog.app  # 注意,这里的blog是blog.py的blog
)

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

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

blog.py文件

import web

urls = (
    "/namelist", "Namelist"
)

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

class Namelist(object):
    def GET(self):
        return "hello"

 1.4 跳转(seeother)和重定向(redirect)

class SamePage(object):
    def GET(self):
        return web.seeother("/namelist")

注意:web.seeother和web.redirect不支持0.3以下版本。

# seeother和redirect的区别
seeother和redirect似乎可以做同样的事情,但通常来说,redirect并不太友好。
因为redirect发送的是301消息——这是永久重定向。因为大多数Web浏览器会缓存新的重定向,所以当我们再次执行该操作时,会自动访问重定向的新网址。
很多时候,这不是我们所想要的结果。所以在提交表单时,尽量使用seeother。
但是在下面要提交的这种场合,用redirect确实对恰当的:我们已经改变了网站的网址结构,但是仍想让用户书签
/收藏夹中的旧网址不生效。

1.5 使用子应用

# 子应用 blog.py

import web

urls = (
    "", "reblog",
    "/(.*)", "blog"
)

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

class reblog: def GET(self): raise web.seeother("/") class blog: def GET(self, path): return "blog" + path
# 主应用 code.py

import web
import blog 

urls = (
    "/blog", blog.app_blog,
    "/(.*)", "index"
)


class index:
    def GET(self, path):
        return "hello" + path


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


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

 1.6 提供XML访问

根据要访问的XML文件(如response.xml)创建一个XML模板。如果XML中有变量,就使用相应的模板标签进行替换。

下面是一个例子:

$def with(code)
<?xml version="1.0">
<RequestNotification-Response>
<Status>$code</Status>
</RequestNotification-Response>

为了提供这个XML,需要创建一个单独的web.py程序(如response.py),他要包含下面的代码。

注意:要用"web.header('Content-Type', 'text/xml')"来告知客户端——正在发送的是一个XML文件。

import web

render = web.template.render('templates/', cache=False)

urls = (
    "/(.*)", "index"
)


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

class index:
    def GET(self, code):
        web.header("Content-Type", "text/xml")
        return render.index(code)

web.webapi.internalerror = web.debugerror

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

 1.7 从post读取原始数据

class RequestHandler:
    def POST(self):
        data = web.data()  # 通过这个方法可以取到数据

第一章内容结束!