web.py

1.web.py簡介

一個很是輕量級的web開發框架,簡單而功能強大
使用Python語言開發web程序
 

2.web.py的安裝

 
web.py的官網 www.webpy.org,上面有相應的文檔和安裝的相關步驟
首先得安裝python,目前web.py只支持pythonw2.7x版本,不支持python3.x版本
能夠使用python的包管理工具pip來安裝web.py,進行命令行,鍵入命令pip install web.py便可成功安裝web.py
 

3. web.py的測試

進行官網 www.webpy.org,將其給出的測試代碼保存到本地,假設命令爲hello.py
  1.  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()
運行這段代碼
python hello.py

 

4. web的開發基礎

WEB開發須要掌握 HTML  CSS JS的基礎知識 
下面開始寫第一個demo程序
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()
View Code

 效果以下圖html

 

5. URL映射

url徹底匹配 /index
url模糊匹配 /post/\d+
url帶組匹配 /post/(\d+)
 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

6.請求的處理

請求參數的獲取 web.input()
請求頭獲取 web.ctx.env
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

7.響應處理

 
 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()

 

更多資料可參考http://webpy.org/
 

 

 
 
 

 

 

 
 

 

 
 

 
 



相關文章
相關標籤/搜索