Python——Web.py詳解
ubuntu安裝Web.py
1 sudo pip install web.py
測試代碼:
1 import web 2 3 urls = ( 4 '/(.*)','hello' 5 ) 6 app = web.application(urls,globals()) 7 8 class hello: 9 def GET(self,name): 10 if not name: 11 name = 'world' 12 return 'hello,'+name+'!' 13 14 if __name__=='__main__': 15 app.run()
类似于flask模板,这里也可以使用文件读写返回html内容:
1 import web 2 3 urls = ( 4 '/(.*)','hello' 5 ) 6 app = web.application(urls,globals()) 7 8 class hello: 9 def GET(self,name): 10 return open(r'index.html','r').read() 11 12 if __name__=='__main__': 13 app.run()
URL映射:
完全匹配: /index
模糊匹配 /post/\d+
带组匹配 /post/(\d+)
1 import web 2 3 urls = ( 4 '/blog/\d+','blog', 5 '/index','index', 6 '/(.*)','hello', 7 ) 8 app = web.application(urls,globals()) 9 10 class index: 11 def GET(self): 12 return 'index' 13 14 class blog: 15 def GET(self): 16 return 'blog' 17 18 class hello: 19 def GET(self,name): 20 return 'hello' 21 22 if __name__=='__main__': 23 app.run()
请求处理:
请求参数获取 : web.input()
hello.html源码:
1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title>User Login</title> 7 </head> 8 9 <body> 10 <div> 11 <h6>User Login</h6> 12 </div> 13 <form action="/blog/123" method="POST"> 14 <h6>用戶名:</h6> 15 <input type="text" name="username"><br> 16 <h6>密碼:</h6> 17 <input type="password" name="password"><br> 18 <input type="submit" name="submit"><br> 19 </form> 20 </body> 21 </html>
app.py源码:
1 import web 2 3 urls = ( 4 '/blog/\d+','blog', 5 '/index','index', 6 '/(.*)','hello', 7 ) 8 app = web.application(urls,globals()) 9 10 class index: 11 def GET(self): 12 r = web.input() 13 return r 14 15 class blog: 16 def POST(self): 17 r = web.input() 18 return r 19 20 class hello: 21 def GET(self,name): 22 return open(r'hello.html','r').read() 23 24 if __name__=='__main__': 25 app.run()
运行结果:
请求头获取 : web.ctx.env
app.py代码如下,hello.html代码如上相同
1 import web 2 3 urls = ( 4 '/blog/\d+','blog', 5 '/index','index', 6 '/(.*)','hello', 7 ) 8 app = web.application(urls,globals()) 9 10 class index: 11 def GET(self): 12 return web.ctx.env 13 14 class blog: 15 def POST(self): 16 r = web.input() 17 return r 18 19 class hello: 20 def GET(self,name): 21 return open(r'hello.html','r').read() 22 23 if __name__=='__main__': 24 app.run()
运行结果: 响应处理:
模板文件读取:render.index("参数")
在py文件同目录下要创建templates文件夹,存放模板。
index是指定模板的名称,参数是html中是所需参数。
1 import web 2 3 render = web.template.render("templates") 4 5 urls = ( 6 '/blog/\d+','blog', 7 '/index','index', 8 '/(.*)','hello', 9 ) 10 app = web.application(urls,globals()) 11 12 class hello: 13 def GET(self,name): 14 return render.hello() 15 16 if __name__=='__main__': 17 app.run()
采用模板就可以不用之前所用的open(r'hello.html','r').read()了。
结果数据获取:model.select("sql")
参考我之前的文章http://www.cnblogs.com/LexMoon/p/Flask_6.html
URL跳转 :web.seeother("/")
1 import web 2 3 urls = ( 4 '/(.*)','hello', 5 ) 6 app = web.application(urls,globals()) 7 8 class hello: 9 def GET(self,name): 10 return web.seeother('http://www.cnblogs.com/LexMoon/') 11 12 if __name__=='__main__': 13 app.run()
作者: AntzUhl
首发地址博客园:http://www.cnblogs.com/LexMoon/
代码均可在Github上找到(求Star) : Github
个人博客 : http://antzuhl.cn/
公众号 |
![]() |
赞助
支付宝 |
微信 |
![]() |
![]() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步