web.py开发详解编程语言
web.py需要使用python2.X,所以安装python版本2.7.9
web.py 是一个轻量级Python web框架,它简单而且功能强大
web.py安装
安装python
(1)使用pip
pip install web.py
安装的目录Python27/Lib/site-packages
(2) IT虾米网下载release版本的web.py
下载下来之后,解压,打开cmd,cd到解压目录下,输入
python setup.py install
查看安装是否成功,pip list
web.py 测试
新建hello.py
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()
进入保存的目录
python hello.py
在浏览器输入http://127.0.0.1:8080/
命令窗口显示
第一部分(‘/’)是一个匹配URL 的正则表达式;第二部分(‘index’)是一个类名,匹配的请求将会被发送过去
若要制定另外的端口使用python code.py 后面添加IP 地址/端口
如:http://192.168.5.239:8080/aa
web.py的输出html页面
import web urls = ( /(.*), hello app = web.application(urls, globals()) class hello: def GET(self, name): return open(raa.html,r).read() if __name__ == "__main__": app.run()
aa.html页面是已有的html页面
web.py学习
1.URL映射
完全匹配
模糊匹配
带组匹配
import web urls = ( /index,index, /blog//d+,blog, /(.*), hello app = web.application(urls, globals()) class index: def GET(self): return index method class blog: def GET(self): return blog method class hello: def GET(self, name): if not name: name = World return Hello, + name + ! if __name__ == "__main__": app.run()
注:
范围大的要放在后面
2.请求处理
请求参数获取
web.input()
请求头获取
web.ctx.env
hello.py
import web urls = ( /index,index, /blog//d+,blog, /(.*), hello app = web.application(urls, globals()) class index: def GET(self): query = web.input() return query class blog: def POST(self): data = web.input() return data class hello: def GET(self, name): return open(rhello.html).read() if __name__ == "__main__": app.run()
hello.html
html head title hello /title head body form action="/blog/123" method="POST" input type="text" name="id" value="" / input type="text" name="name" value="" / input type="submit" value="submit" /form /body /html
表单提交后
修改代码获取请求头
import web urls = ( /index,index, /blog//d+,blog, /(.*), hello app = web.application(urls, globals()) class index: def GET(self): query = web.input() return query class blog: def POST(self): data = web.input() return data def GET(self): data1 = web.ctx.env return data1 class hello: def GET(self, name): return open(rhello.html).read() if __name__ == "__main__": app.run()
3.相应处理
(1)模板文件读取
render.index( 参数 )
hello.py
import web render = web.template.render("templates") urls = ( /index,index, /blog//d+,blog, /(.*), hello app = web.application(urls, globals()) class index: def GET(self): query = web.input() return query class blog: def POST(self): data = web.input() return data def GET(self): data1 = web.ctx.env return data1 class hello: def GET(self, name): return render.hello1(name) if __name__ == "__main__": app.run()
在hello.py同级目录下,存在templates/hello1.html
hello1.html
$def with(name) html head title hello1 /title head body h1 hello1,$name /h1 form action="/blog/123" method="POST" input type="text" name="id" value="" / input type="text" name="name" value="" / input type="submit" value="submit" /form /body /html
(2)结果数据处理
model.select( sql)
安装已编译版本 下载MySQldb ,安装已编译版本
goods.py
import web import MySQLdb import MySQLdb.cursors render = web.template.render("templates") urls = ( /goods,goods app = web.application(urls, globals()) class goods: def GET(self): conn = MySQLdb.connect(host=127.0.0.1,user=root,passwd=root,db=mshop_dev,port=3306,charset="utf8",cursor "select * from mshop_goods limit 5") r=cur.fetchall() cur.close() conn.close() print r return render.goods(r) if __name__ == "__main__": app.run()
goods.html
$def with(r) html head meta charset="utf-8" / title goods /title head body h1 商品列表 /h1 $for l in r: li $l.get(goods_id),$l.get(goods_name)= $l.get(store_name) li /ul /body /html
(3)URL跳转
web.seeother( / )
import web import MySQLdb import MySQLdb.cursors render = web.template.render("templates") urls = ( /index,index, /goods,goods, /(.*), hello app = web.application(urls, globals()) class index: def GET(self): return web.seeother("/goods") class goods: def GET(self): conn = MySQLdb.connect(host=127.0.0.1,user=root,passwd=root,db=mshop_dev,port=3306,charset="utf8",cursor "select * from mshop_goods limit 5") r=cur.fetchall() cur.close() conn.close() print r return render.goods(r) class hello: def GET(self, name): return web.seeother("http://baidu.com") if __name__ == "__main__": app.run()
输入http://127.0.0.1/index跳转到http://127.0.0.1:8080/goods
输入http://127.0.0.1/hello跳转到https://www.baidu.com/
注:
web.py的静态文件必须放在static文件夹下面
web.py并不具备部署网站的能力,因此对于web.py程序只能在本地访问,如果要进行部署必须要使用apache、nginx、lighttped
通过FastCGI结合lighttpd是web.py,通过该方法可以处理百万次的点击
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/20510.html
cgomysqlpython相关文章
- 如何利用eclipse创建一个java web项目?「建议收藏」
- Web Components 详解
- ASP.NET WEB——项目创建与文件上传操作
- Flutter Web - 优雅的兼容 Flutter App 代码
- Web标准与前端开发 - 笔记
- 使用CXF开发简单的Web Service-HelloWorld(二)详解编程语言
- 建立一个node.js服务器(使用express搭建第一个Web环境)详解编程语言
- Python Web开发——学习笔记详解编程语言
- Web 前端颜色值–字体–使用,整理整理详解编程语言
- java的web配置文件的“的说明详解编程语言
- Spring Boot2.0之web开发详解编程语言
- 上搭建web服务实现Web服务——学习Linux基础技能(在linux基础)
- SAP_Web_Service开发配置详解编程语言
- Spring MVC框架处理Web请求的基本流程详解编程语言
- 服务Linux下访问Web服务的指南(linux访问web)
- 快速部署Linux环境下Web项目(linux部署web项目)
- SAP PI 配置soap web service详解编程语言
- Web项目下使用Redis加速优化(web项目 redis)
- Web端登录Redis提高数据库性能(web登录redis作用)
- 红色的记忆Redis记录Web日志(redis记录web日志)
- 分页栏的web标准实现
- Winform实现抓取web页面内容的方法