日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
用Python寫一個(gè)簡(jiǎn)單的Web框架

  •  一、概述
  • 二、從demo_app開始
  • 三、WSGI中的application
  • 四、區(qū)分URL
  • 五、重構(gòu)
    • 1、正則匹配URL
    • 2、DRY
    • 3、抽象出框架
  • 六、參考

一、概述

在Python中,WSGI(Web Server Gateway Interface)定義了Web服務(wù)器與Web應(yīng)用(或Web框架)之間的標(biāo)準(zhǔn)接口。在WSGI的規(guī)范下,各種各樣的Web服務(wù)器和Web框架都可以很好的交互。

由于WSGI的存在,用Python寫一個(gè)簡(jiǎn)單的Web框架也變得非常容易。然而,同很多其他的強(qiáng)大軟件一樣,要實(shí)現(xiàn)一個(gè)功能豐富、健壯高效的Web框架并非易事;如果您打算這么做,可能使用一個(gè)現(xiàn)成的Web框架(如 Django、Tornado、web.py 等)會(huì)是更合適的選擇。

本文嘗試寫一個(gè)類似web.py的Web框架。好吧,我承認(rèn)我夸大其辭了:首先,web.py并不簡(jiǎn)單;其次,本文只重點(diǎn)實(shí)現(xiàn)了 URL調(diào)度(URL dispatch)部分。

二、從demo_app開始

首先,作為一個(gè)初步體驗(yàn),我們可以借助 wsgiref.simple_server 來搭建一個(gè)簡(jiǎn)單無比(trivial)的Web應(yīng)用:

 
 
 
 
  1. #!/usr/bin/env python 
  2.  
  3. # -*- coding: utf-8 -*- 
  4.  
  5.   
  6.  
  7. from wsgiref.simple_server import make_server, demo_app 
  8.  
  9.   
  10.  
  11. httpd = make_server('', 8086, demo_app) 
  12.  
  13. sa = httpd.socket.getsockname() 
  14.  
  15. print 'http://{0}:{1}/'.format(*sa) 
  16.  
  17.   
  18.  
  19. # Respond to requests until process is killed 
  20.  
  21. httpd.serve_forever()  

運(yùn)行腳本:

 
 
 
 
  1. $ python code.py 
  2.  
  3. http://0.0.0.0:8086/  

打開瀏覽器,輸入http://0.0.0.0:8086/后可以看到:一行”Hello world!” 和 眾多環(huán)境變量值。

三、WSGI中的application

WSGI中規(guī)定:application是一個(gè) 可調(diào)用對(duì)象(callable object),它接受 environ 和 start_response 兩個(gè)參數(shù),并返回一個(gè) 字符串迭代對(duì)象。

其中,可調(diào)用對(duì)象 包括 函數(shù)、方法、類 或者 具有__call__方法的 實(shí)例;environ 是一個(gè)字典對(duì)象,包括CGI風(fēng)格的環(huán)境變量(CGI-style environment variables)和 WSGI必需的變量(WSGI-required variables);start_response 是一個(gè)可調(diào)用對(duì)象,它接受兩個(gè) 常規(guī)參數(shù)(status,response_headers)和 一個(gè) 默認(rèn)參數(shù)(exc_info);字符串迭代對(duì)象 可以是 字符串列表、生成器函數(shù) 或者 具有__iter__方法的可迭代實(shí)例。更多細(xì)節(jié)參考 Specification Details。

The Application/Framework Side 中給出了一個(gè)典型的application實(shí)現(xiàn):

 
 
 
 
  1. #!/usr/bin/env python 
  2.  
  3. # -*- coding: utf-8 -*- 
  4.  
  5.   
  6.  
  7. """application.py""" 
  8.  
  9.   
  10.  
  11. def simple_app(environ, start_response): 
  12.  
  13.     """Simplest possible application object""" 
  14.  
  15.     status = '200 OK' 
  16.  
  17.     response_headers = [('Content-type', 'text/plain')] 
  18.  
  19.     start_response(status, response_headers) 
  20.  
  21.     return ['Hello world!\n']  

現(xiàn)在用simple_app來替換demo_app:

 
 
 
 
  1. #!/usr/bin/env python 
  2.  
  3. # -*- coding: utf-8 -*- 
  4.  
  5.   
  6.  
  7. """code.py""" 
  8.  
  9.   
  10.  
  11. from wsgiref.simple_server import make_server 
  12.  
  13. from application import simple_app as app 
  14.  
  15.   
  16.  
  17. if __name__ == '__main__': 
  18.  
  19.     httpd = make_server('', 8086, app) 
  20.  
  21.     sa = httpd.socket.getsockname() 
  22.  
  23.     print 'http://{0}:{1}/'.format(*sa) 
  24.  
  25.   
  26.  
  27.     # Respond to requests until process is killed 
  28.  
  29.     httpd.serve_forever()  

運(yùn)行腳本code.py后,訪問http://0.0.0.0:8086/就可以看到那行熟悉的句子:Hello world!

四、區(qū)分URL

倒騰了一陣子后,您會(huì)發(fā)現(xiàn)不管如何改變URL中的path部分,得到的響應(yīng)都是一樣的。因?yàn)閟imple_app只識(shí)別host+port部分。

為了對(duì)URL中的path部分進(jìn)行區(qū)分處理,需要修改application.py的實(shí)現(xiàn)。

首先,改用 類 來實(shí)現(xiàn)application:

 
 
 
 
  1. #!/usr/bin/env python 
  2.  
  3. # -*- coding: utf-8 -*- 
  4.  
  5.   
  6.  
  7. """application.py""" 
  8.  
  9.   
  10.  
  11. class my_app: 
  12.  
  13.     def __init__(self, environ, start_response): 
  14.  
  15.         self.environ = environ 
  16.  
  17.         self.start = start_response 
  18.  
  19.   
  20.  
  21.     def __iter__(self): 
  22.  
  23.         status = '200 OK' 
  24.  
  25.         response_headers = [('Content-type', 'text/plain')] 
  26.  
  27.         self.start(status, response_headers) 
  28.  
  29.         yield "Hello world!\n"  

然后,增加對(duì)URL中path部分的區(qū)分處理:

 
 
 
 
  1. #!/usr/bin/env python 
  2.  
  3. # -*- coding: utf-8 -*- 
  4.  
  5.   
  6.  
  7. """application.py""" 
  8.  
  9.   
  10.  
  11. class my_app: 
  12.  
  13.     def __init__(self, environ, start_response): 
  14.  
  15.         self.environ = environ 
  16.  
  17.         self.start = start_response 
  18.  
  19.   
  20.  
  21.     def __iter__(self): 
  22.  
  23.         path = self.environ['PATH_INFO'] 
  24.  
  25.         if path == "/": 
  26.  
  27.             return self.GET_index() 
  28.  
  29.         elif path == "/hello": 
  30.  
  31.             return self.GET_hello() 
  32.  
  33.         else: 
  34.  
  35.             return self.notfound() 
  36.  
  37.   
  38.  
  39.     def GET_index(self): 
  40.  
  41.         status = '200 OK' 
  42.  
  43.         response_headers = [('Content-type', 'text/plain')] 
  44.  
  45.         self.start(status, response_headers) 
  46.  
  47.         yield "Welcome!\n" 
  48.  
  49.   
  50.  
  51.     def GET_hello(self): 
  52.  
  53.         status = '200 OK' 
  54.  
  55.         response_headers = [('Content-type', 'text/plain')] 
  56.  
  57.         self.start(status, response_headers) 
  58.  
  59.         yield "Hello world!\n" 
  60.  
  61.   
  62.  
  63.     def notfound(self): 
  64.  
  65.         status = '404 Not Found' 
  66.  
  67.         response_headers = [('Content-type', 'text/plain')] 
  68.  
  69.         self.start(status, response_headers) 
  70.  
  71.         yield "Not Found\n"  

修改code.py中的from application import simple_app as app,用my_app來替換simple_app后即可體驗(yàn)效果。

五、重構(gòu)

上面的代碼雖然奏效,但是在編碼風(fēng)格和靈活性方面有很多問題,下面逐步對(duì)其進(jìn)行重構(gòu)。

1、正則匹配URL

消除URL硬編碼,增加URL調(diào)度的靈活性:

 
 
 
 
  1. #!/usr/bin/env python 
  2.  
  3. # -*- coding: utf-8 -*- 
  4.  
  5.   
  6.  
  7. """application.py""" 
  8.  
  9.   
  10.  
  11. import re ##########修改點(diǎn) 
  12.  
  13.   
  14.  
  15. class my_app: 
  16.  
  17.   
  18.  
  19.     urls = ( 
  20.  
  21.         ("/", "index"), 
  22.  
  23.         ("/hello/(.*)", "hello"), 
  24.  
  25.     ) ##########修改點(diǎn) 
  26.  
  27.   
  28.  
  29.     def __init__(self, environ, start_response): 
  30.  
  31.         self.environ = environ 
  32.  
  33.         self.start = start_response 
  34.  
  35.   
  36.  
  37.     def __iter__(self): ##########修改點(diǎn) 
  38.  
  39.         path = self.environ['PATH_INFO'] 
  40.  
  41.         method = self.environ['REQUEST_METHOD'] 
  42.  
  43.   
  44.  
  45.         for pattern, name in self.urls: 
  46.  
  47.             m = re.match('^' + pattern + '$', path) 
  48.  
  49.             if m: 
  50.  
  51.                 # pass the matched groups as arguments to the function 
  52.  
  53.                 args = m.groups() 
  54.  
  55.                 funcname = method.upper() + '_' + name 
  56.  
  57.                 if hasattr(self, funcname): 
  58.  
  59.                     func = getattr(self, funcname) 
  60.  
  61.                     return func(*args) 
  62.  
  63.   
  64.  
  65.         return self.notfound() 
  66.  
  67.   
  68.  
  69.     def GET_index(self): 
  70.  
  71.         status = '200 OK' 
  72.  
  73.         response_headers = [('Content-type', 'text/plain')] 
  74.  
  75.         self.start(status, response_headers) 
  76.  
  77.         yield "Welcome!\n" 
  78.  
  79.   
  80.  
  81.     def GET_hello(self, name): ##########修改點(diǎn) 
  82.  
  83.         status = '200 OK' 
  84.  
  85.         response_headers = [('Content-type', 'text/plain')] 
  86.  
  87.         self.start(status, response_headers) 
  88.  
  89.         yield "Hello %s!\n" % name 
  90.  
  91.   
  92.  
  93.     def notfound(self): 
  94.  
  95.         status = '404 Not Found' 
  96.  
  97.         response_headers = [('Content-type', 'text/plain')] 
  98.  
  99.         self.start(status, response_headers) 
  100.  
  101.         yield "Not Found\n"  

2、DRY

消除GET_*方法中的重復(fù)代碼,并且允許它們返回字符串:

 
 
 
 
  1. #!/usr/bin/env python 
  2.  
  3. # -*- coding: utf-8 -*- 
  4.  
  5.   
  6.  
  7. """application.py""" 
  8.  
  9.   
  10.  
  11. import re 
  12.  
  13.   
  14.  
  15. class my_app: 
  16.  
  17.   
  18.  
  19.     urls = ( 
  20.  
  21.         ("/", "index"), 
  22.  
  23.         ("/hello/(.*)", "hello"), 
  24.  
  25.     ) 
  26.  
  27.   
  28.  
  29.     def __init__(self, environ, start_response): ##########修改點(diǎn) 
  30.  
  31.         self.environ = environ 
  32.  
  33.         self.start = start_response 
  34.  
  35.         self.status = '200 OK' 
  36.  
  37.         self._headers = [] 
  38.  
  39.   
  40.  
  41.     def __iter__(self): ##########修改點(diǎn) 
  42.  
  43.         result = self.delegate() 
  44.  
  45.         self.start(self.status, self._headers) 
  46.  
  47.   
  48.  
  49.         # 將返回值result(字符串 或者 字符串列表)轉(zhuǎn)換為迭代對(duì)象 
  50.  
  51.         if isinstance(result, basestring): 
  52.  
  53.             return iter([result]) 
  54.  
  55.         else: 
  56.  
  57.             return iter(result) 
  58.  
  59.   
  60.  
  61.     def delegate(self): ##########修改點(diǎn) 
  62.  
  63.         path = self.environ['PATH_INFO'] 
  64.  
  65.         method = self.environ['REQUEST_METHOD'] 
  66.  
  67.   
  68.  
  69.         for pattern, name in self.urls: 
  70.  
  71.             m = re.match('^' + pattern + '$', path) 
  72.  
  73.             if m: 
  74.  
  75.                 # pass the matched groups as arguments to the function 
  76.  
  77.                 args = m.groups() 
  78.  
  79.                 funcname = method.upper() + '_' + name 
  80.  
  81.                 if hasattr(self, funcname): 
  82.  
  83.                     func = getattr(self, funcname) 
  84.  
  85.                     return func(*args) 
  86.  
  87.   
  88.  
  89.         return self.notfound() 
  90.  
  91.   
  92.  
  93.     def header(self, name, value): ##########修改點(diǎn) 
  94.  
  95.         self._headers.append((name, value)) 
  96.  
  97.   
  98.  
  99.     def GET_index(self): ##########修改點(diǎn) 
  100.  
  101.         self.header('Content-type', 'text/plain') 
  102.  
  103.         return "Welcome!\n" 
  104.  
  105.   
  106.  
  107.     def GET_hello(self, name): ##########修改點(diǎn) 
  108.  
  109.         self.header('Content-type', 'text/plain') 
  110.  
  111.         return "Hello %s!\n" % name 
  112.  
  113.   
  114.  
  115.     def notfound(self): ##########修改點(diǎn) 
  116.  
  117.         self.status = '404 Not Found' 
  118.  
  119.         self.header('Content-type', 'text/plain') 
  120.  
  121.         return "Not Found\n"  

3、抽象出框架

為了將類my_app抽象成一個(gè)獨(dú)立的框架,需要作出以下修改:

  • 剝離出其中的具體處理細(xì)節(jié):urls配置 和 GET_*方法(改成在多個(gè)類中實(shí)現(xiàn)相應(yīng)的GET方法)
  • 把方法header實(shí)現(xiàn)為類方法(classmethod),以方便外部作為功能函數(shù)調(diào)用
  • 改用 具有__call__方法的 實(shí)例 來實(shí)現(xiàn)application

修改后的application.py(最終版本):

 
 
 
 
  1. #!/usr/bin/env python 
  2.  
  3. # -*- coding: utf-8 -*- 
  4.  
  5.   
  6.  
  7. """application.py""" 
  8.  
  9.   
  10.  
  11. import re 
  12.  
  13.   
  14.  
  15. class my_app: 
  16.  
  17.     """my simple web framework""" 
  18.  
  19.   
  20.  
  21.     headers = [] 
  22.  
  23.   
  24.  
  25.     def __init__(self, urls=(), fvars={}): 
  26.  
  27.         self._urls = urls 
  28.  
  29.         self._fvars = fvars 
  30.  
  31.   
  32.  
  33.     def __call__(self, environ, start_response): 
  34.  
  35.         self._status = '200 OK' # 默認(rèn)狀態(tài)OK 
  36.  
  37.         del self.headers[:] # 清空上一次的headers 
  38.  
  39.   
  40.  
  41.         result = self._delegate(environ) 
  42.  
  43.         start_response(self._status, self.headers) 
  44.  
  45.   
  46.  
  47.         # 將返回值result(字符串 或者 字符串列表)轉(zhuǎn)換為迭代對(duì)象 
  48.  
  49.         if isinstance(result, basestring): 
  50.  
  51.             return iter([result]) 
  52.  
  53.         else: 
  54.  
  55.             return iter(result) 
  56.  
  57.   
  58.  
  59.     def _delegate(self, environ): 
  60.  
  61.         path = environ['PATH_INFO'] 
  62.  
  63.         method = environ['REQUEST_METHOD'] 
  64.  
  65.   
  66.  
  67.         for pattern, name in self._urls: 
  68.  
  69.             m = re.match('^' + pattern + '$', path) 
  70.  
  71.             if m: 
  72.  
  73.                 # pass the matched groups as arguments to the function 
  74.  
  75.                 args = m.groups() 
  76.  
  77.                 funcname = method.upper() # 方法名大寫(如GET、POST) 
  78.  
  79.                 klass = self._fvars.get(name) # 根據(jù)字符串名稱查找類對(duì)象 
  80.  
  81.                 if hasattr(klass, funcname): 
  82.  
  83.                     func = getattr(klass, funcname) 
  84.  
  85.                     return func(klass(), *args) 
  86.  
  87.   
  88.  
  89.         return self._notfound() 
  90.  
  91.   
  92.  
  93.     def _notfound(self): 
  94.  
  95.         self._status = '404 Not Found' 
  96.  
  97.         self.header('Content-type', 'text/plain') 
  98.  
  99.         return "Not Found\n" 
  100.  
  101.   
  102.  
  103.     @classmethod 
  104.  
  105.     def header(cls, name, value): 
  106.  
  107.         cls.headers.append((name, value))  

對(duì)應(yīng)修改后的code.py(最終版本):

 
 
 
 
  1. #!/usr/bin/env python 
  2.  
  3. # -*- coding: utf-8 -*- 
  4.  
  5.   
  6.  
  7. """code.py""" 
  8.  
  9.   
  10.  
  11. from application import my_app 
  12.  
  13.   
  14.  
  15. urls = ( 
  16.  
  17.     ("/", "index"), 
  18.  
  19.     ("/hello/(.*)", "hello"), 
  20.  
  21.  
  22.   
  23.  
  24. wsgiapp = my_app(urls, globals()) 
  25.  
  26.   
  27.  
  28. class index: 
  29.  
  30.     def GET(self): 
  31.  
  32.         my_app.header('Content-type', 'text/plain') 
  33.  
  34.         return "Welcome!\n" 
  35.  
  36.   
  37.  
  38. class hello: 
  39.  
  40.     def GET(self, name): 
  41.  
  42.         my_app.header('Content-type', 'text/plain') 
  43.  
  44.         return "Hello %s!\n" % name 
  45.  
  46.   
  47.  
  48. if __name__ == '__main__': 
  49.  
  50.     from wsgiref.simple_server import make_server 
  51.  
  52.     httpd = make_server('', 8086, wsgiapp) 
  53.  
  54.   
  55.  
  56.     sa = httpd.socket.getsockname() 
  57.  
  58.     print 'http://{0}:{1}/'.format(*sa) 
  59.  
  60.   
  61.  
  62.     # Respond to requests until process is killed 
  63.  
  64.     httpd.serve_forever()  

當(dāng)然,您還可以在code.py中配置更多的URL映射,并實(shí)現(xiàn)相應(yīng)的類來對(duì)請(qǐng)求作出響應(yīng)。

六、參考

本文主要參考了 How to write a web framework in Python(作者 anandology 是web.py代碼的兩位維護(hù)者之一,另一位則是大名鼎鼎卻英年早逝的 Aaron Swartz),在此基礎(chǔ)上作了一些調(diào)整和修改,并摻雜了自己的一些想法。

如果您還覺得意猶未盡,Why so many Python web frameworks? 也是一篇很好的文章,也許它會(huì)讓您對(duì)Python中Web框架的敬畏之心蕩然無存:-)


網(wǎng)站名稱:用Python寫一個(gè)簡(jiǎn)單的Web框架
本文URL:http://www.5511xx.com/article/djsdeje.html