web.py

web.py是什么?

是一个轻量级的python web框架,它简单而且功能强大。

web.py的安装

1、安装python,配置环境变量

2、命令行输入命令 pip install web.py

url映射

精确匹配 /index,模糊匹配 /blog/d+,组匹配 /(.*)

请求处理

获取入参 web.input() 获取请求头 web.ctx.env

响应处理

1、模板文件读取:render.blog1()

$def with (r)
<html lang="en">
<head>
<meta charset="UTF-8">
<title>blog1</title>
</head>
<body>
<h1>文章列表</h1>
<ul>$for l in r:
<li>$l.get("title") => $l.get("content")</li>
</ul>
</body>
</html>

2、结果数据获取:model.select("sql")

conn = pymysql.connect(host="localhost",user="root",passwd="root",db="mysql",charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
cur= conn.cursor()
cur.execute("select * from articles")
r = cur.fetchall()
cur.close()
conn.close()
print(r)

3、url跳转:外部网页跳转:web.seeother("http://www.baidu.com"),内部网页跳转:render.blog1(r)

原文地址:https://www.cnblogs.com/niuyg928/p/12240600.html