1 import web 2 urls =( 3 '/(.*)','hello' 4 ) 5 app = web.application(urls, globals()) 6 class hello: 7 def GET(self, name): 8 if not name: 9 name ='World' 10 return'Hello, '+ name +'!' 11 if __name__ =="__main__": 12 app.run()
import web urls =( '/(.*)','hello' ) app = web.application(urls, globals()) class hello: def GET(self, name): file = open('Welcome to web.py! (web.py).html','r') return file.read() if __name__ =="__main__": app.run()
效果以下圖html
1 import web 2 urls =( 3 '/index','index', 4 '/blog/\d+','blog', 5 '/(.*)','hello' 6 ) 7 app = web.application(urls, globals()) 8 class index: 9 def GET(self): 10 return"index" 11 class blog: 12 def GET(self): 13 return'blog' 14 class hello: 15 def GET(self, name ): 16 if name =="": 17 name ='world' 18 return'hello,'+name 19 if __name__ =="__main__": 20 app.run()
效果python
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): return web.input() def GET(self): return web.ctx.env class hello: def GET(self, name ): return open('1.html','r').read() if __name__ =="__main__": app.run()
效果web
1 import web 2 import MySQLdb 3 import MySQLdb.cursors 4 5 render = web.template.render("templates") 6 7 8 urls = ( 9 '/article','article', 10 '/index','index', 11 '/blog/\d+','blog', 12 '/(.*)','hello' 13 ) 14 app = web.application(urls, globals()) 15 16 class index: 17 def GET(self): 18 return web.seeother("http://www.baidu.com") 19 class blog: 20 def POST(self): 21 return web.input() 22 def GET(self): 23 return web.ctx.env 24 class hello: 25 def GET(self, name ): 26 return render.hello2(name) 27 class article: 28 def GET(self): 29 conn = MySQLdb.Connect(host="localhost",port=3306,user='root',passwd='123456',db='test') 30 cur=conn.cursor() 31 cur.execute('select * from articles') 32 r = cur.fetchall() 33 print cur.rowcount 34 35 cur.close() 36 conn.close() 37 return render.article(r) 38 39 if __name__ == "__main__": 40 app.run()