【Python】web.py模块
本文最后更新于 1250 天前,其中的信息可能已经有所发展或是发生改变。

web.py模块

web.py模块是一个轻量级Python web框架,它简单而且功能强大。
官方的中文文档:http://webpy.org/cookbook/index.zh-cn
pip下载模块:pip install web.py
(注意是web.py而不是web)

1.第一个简单的案例。

官方的第一个案例:

import web

urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:
    def GET(self, name):
        if not name:
            name = 'World'
        return 'Hello, ' + name + '!'

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

2.返回html。

读取html文件并返回。

import codecs

urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:
    def GET(self, name):
        html = codecs.open("demo.html","r",'utf-8').read()
        return html

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

3.URL映射。

URL映射的三种模式:完全匹配、模糊匹配、带组匹配。
接受GET、POST的参数与请求头的获取。

import web
import codecs

#URL映射

urls = (
    '/index','index',
    '/login','login',
    '/form','form',
    '/header','header',
    '/benzhu/\d+','benzhu',
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

#URL完全匹配
class index:
    def GET(self):
        html = codecs.open('demo.html', 'r', 'utf-8').read()
        return html

#form表单
class login:
    def GET(self):
        return codecs.open('demo2.html', 'r', 'utf-8').read()

#接受参数
class form:
    def POST(self):
        data = web.input()
        return data;
    def GET(self):
        data = web.input()
        return data;

#请求头
class header:
    def GET(self):
        return web.ctx.env

#URL模糊匹配
class benzhu:
    def GET(self):
        return 'benzhu get'
    def POST(self):
        return 'benzhu post'

#URL带组匹配
class hello:
    def GET(self, name):
        if not name:
            name = 'World'
        return 'Hello, ' + name + '!'

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

表单:

<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <title>提交表单</title>
</head>
<body>
    <form action="/form" method="post">
        用户名:<input type="text" name="id" value="" />
        密码:<input type="text" name="password" value="" />
        <input type="submit" value="提交"/>
    </form>
</body>
</html>

4.扩展。

使用模板文件进行访问。
从数据库读取数据并返回。
自定义跳转。

# -*- coding: UTF-8 -*-

import web
import pymysql

urls = (
    '/benzhu','benzhu',
    '/index/(.*)','index',
)

render = web.template.render('templates')
app = web.application(urls, globals())

#跳转
class benzhu:
    def GET(self):
        return web.seeother('/index/xiaozhu')

#使用模板文件访问
#传输参数和接收参数
class index:
    def GET(self, username):
        print(username)
        # 创建连接
        conn = pymysql.Connect(host="127.0.0.1", port=3306, user="root", passwd="123456", db="test")
        # 创建游标
        cursor = conn.cursor()
        # SQL语句查
        sql = "select * from user"
        # 读取数据
        cursor.execute(sql)
        r = cursor.fetchall()
        print(r)
        # 关闭游标
        cursor.close()
        # 关闭连接
        conn.close()
        return render.demo2(username,r)


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

html文件:
注意要放在templates文件夹下。
templates/demo2.html

$def with(username2,r)
<!--参数接收要放在第一行 -->
<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <title>提交表单</title>
</head>
<body>
    <form action="/form" method="post">
        用户名:<input type="text" name="id" value="$username2" />
        密码:<input type="text" name="password" value="" />
        <input type="submit" value="提交"/>
    </form>
    <h1>用户列表</h1>
    <ul>
    $for l in r:
        <li>$l[0]  ||  $l[1]</li>
    </ul>
</body>
</html>
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇