成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

用Python寫一個簡單的Web框架

開發 后端
本文嘗試寫一個類似web.py的Web框架。好吧,我承認我夸大其辭了:首先,web.py并不簡單;其次,本文只重點實現了 URL調度(URL dispatch)部分。

用Python寫一個簡單的Web框架

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

一、概述

在Python中,WSGI(Web Server Gateway Interface)定義了Web服務器與Web應用(或Web框架)之間的標準接口。在WSGI的規范下,各種各樣的Web服務器和Web框架都可以很好的交互。

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

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

二、從demo_app開始

首先,作為一個初步體驗,我們可以借助 wsgiref.simple_server 來搭建一個簡單無比(trivial)的Web應用:

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

運行腳本:

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

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

三、WSGI中的application

WSGI中規定:application是一個 可調用對象(callable object),它接受 environ 和 start_response 兩個參數,并返回一個 字符串迭代對象。

其中,可調用對象 包括 函數、方法、類 或者 具有__call__方法的 實例;environ 是一個字典對象,包括CGI風格的環境變量(CGI-style environment variables)和 WSGI必需的變量(WSGI-required variables);start_response 是一個可調用對象,它接受兩個 常規參數(status,response_headers)和 一個 默認參數(exc_info);字符串迭代對象 可以是 字符串列表、生成器函數 或者 具有__iter__方法的可迭代實例。更多細節參考 Specification Details。

The Application/Framework Side 中給出了一個典型的application實現:

  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' 

現在用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()  

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

四、區分URL

倒騰了一陣子后,您會發現不管如何改變URL中的path部分,得到的響應都是一樣的。因為simple_app只識別host+port部分。

為了對URL中的path部分進行區分處理,需要修改application.py的實現。

首先,改用 類 來實現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"  

然后,增加對URL中path部分的區分處理:

  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后即可體驗效果。

五、重構

上面的代碼雖然奏效,但是在編碼風格和靈活性方面有很多問題,下面逐步對其進行重構。

1、正則匹配URL

消除URL硬編碼,增加URL調度的靈活性:

  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): 
  30.  
  31.         self.environ = environ 
  32.  
  33.         self.start = start_response 
  34.  
  35.   
  36.  
  37.     def __iter__(self): ##########修改點 
  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): ##########修改點 
  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_*方法中的重復代碼,并且允許它們返回字符串:

  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): ##########修改點 
  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): ##########修改點 
  42.  
  43.         result = self.delegate() 
  44.  
  45.         self.start(self.status, self._headers) 
  46.  
  47.   
  48.  
  49.         # 將返回值result(字符串 或者 字符串列表)轉換為迭代對象 
  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): ##########修改點 
  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): ##########修改點 
  94.  
  95.         self._headers.append((name, value)) 
  96.  
  97.   
  98.  
  99.     def GET_index(self): ##########修改點 
  100.  
  101.         self.header('Content-type''text/plain'
  102.  
  103.         return "Welcome!\n" 
  104.  
  105.   
  106.  
  107.     def GET_hello(self, name): ##########修改點 
  108.  
  109.         self.header('Content-type''text/plain'
  110.  
  111.         return "Hello %s!\n" % name 
  112.  
  113.   
  114.  
  115.     def notfound(self): ##########修改點 
  116.  
  117.         self.status = '404 Not Found' 
  118.  
  119.         self.header('Content-type''text/plain'
  120.  
  121.         return "Not Found\n"  

3、抽象出框架

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

  • 剝離出其中的具體處理細節:urls配置 和 GET_*方法(改成在多個類中實現相應的GET方法)
  • 把方法header實現為類方法(classmethod),以方便外部作為功能函數調用
  • 改用 具有__call__方法的 實例 來實現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' # 默認狀態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(字符串 或者 字符串列表)轉換為迭代對象 
  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) # 根據字符串名稱查找類對象 
  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))  

對應修改后的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()  

當然,您還可以在code.py中配置更多的URL映射,并實現相應的類來對請求作出響應。

六、參考

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

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

責任編輯:龐桂玉 來源: Python開發者
相關推薦

2015-10-12 16:45:26

NodeWeb應用框架

2016-09-14 17:48:44

2016-12-20 13:55:52

2022-04-01 15:18:42

Web 框架網絡通信

2020-07-20 10:00:52

Python翻譯工具命令行

2018-10-31 10:11:24

Python編程語言語音播放

2010-04-19 17:21:36

Oracle寫文件

2022-03-24 14:42:19

Python編程語言

2020-09-29 15:08:47

Go UI框架開發

2019-09-23 09:11:02

Python文本編輯器操作系統

2023-04-07 15:45:13

Emojicode開源編碼語言

2021-05-14 10:45:21

PythonNoSQL數據庫

2017-05-18 12:16:03

LinuxPythonNoSql

2021-08-04 11:55:45

Python天氣查詢PySide2

2023-04-10 14:20:47

ChatGPTRESTAPI

2023-05-10 08:05:41

GoWeb應用

2009-05-08 09:32:27

JavaWeb編程框架

2020-06-04 12:55:44

PyTorch分類器神經網絡

2019-05-14 12:30:07

PythonPygame游戲框架

2014-04-14 15:54:00

print()Web服務器
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 一区二区三区四区在线视频 | 成人在线一区二区 | 狠狠干狠狠操 | 黄色小视频大全 | 91久久精品国产 | 男人亚洲天堂 | 天天干干 | 国产综合精品 | 日本超碰| 波多野结衣一区二区三区在线观看 | 久久国产99 | 中文字幕亚洲视频 | 国产精品91视频 | 亚洲综合字幕 | 久久久精品一区 | 日本成人免费观看 | 日韩免费网站 | 欧美在线视频网站 | 国产精品国产精品国产专区不片 | 精品日本久久久久久久久久 | 欧美一区二区三区在线观看 | 男女啪啪网址 | 国产精品毛片无码 | 中文字幕第一页在线 | 亚洲3级| 国产成人精品一区二区三区在线观看 | 911网站大全在线观看 | 久久综合av | 野狼在线社区2017入口 | 免费在线日韩 | 综合精品 | 日本精品一区 | 免费一级做a爰片久久毛片潮喷 | 国产成人综合一区二区三区 | 国产一区二区久久久 | 成人依人| 欧美一级一 | 999久久久久久久久6666 | 亚洲精品久久久久久久久久久久久 | 日韩国产在线 | 免费在线观看黄色av |