web.py須要使用python2.X,因此安裝python版本2.7.9html
web.py 是一個輕量級Python web框架,它簡單並且功能強大python
web.py安裝nginx
安裝pythongit
(1)使用pipgithub
pip install web.pyweb
安裝的目錄Python27\Lib\site-packages正則表達式
(2) https://github.com/webpy/webpy下載release版本的web.pysql
下載下來以後,解壓,打開cmd,cd到解壓目錄下,輸入apache
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(r'aa.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(r'hello.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(r'hello.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",cursorclass=MySQLdb.cursors.DictCursor) cur=conn.cursor() cur.execute("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> <ul> $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",cursorclass=MySQLdb.cursors.DictCursor) cur=conn.cursor() cur.execute("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,經過該方法能夠處理百萬次的點擊