python wsgiref源碼解析
python web開發(fā)中http請求的處理流程通常是: web-browser , web-server , wsgi 和 web-application四個環(huán)節(jié), 我們學習過基于bottle實現的web-application,也學習了http.server。再完成python3源碼中自帶的wsgiref的庫,就可以拼接最后一個環(huán)節(jié)wsgi。本文會分下面幾個部分:
- wsgi相關概念
- cgi示例
- wsgiref源碼
- wsgi小結
- 小技巧
wsgi 相關概念
CGI
CGI(Common Gateway Interface)通用網關接口。1993年由美國NCSA(National Center for Supercomputing Applications)發(fā)明。它具有簡單易用、語言無關的特點。雖然今天已經少有人直接使用CGI進行編程,但它仍被主流的Web服務器,如Apache、IIS、Nginx等廣泛支持。

CGI提供了一種接口規(guī)范,可以讓應用程序, 一般是各種腳本語言,比如perl, php, python等來擴展web服務,讓服務動態(tài)起來。
WSGI
WSGI(Web Server Gateway Interface)web服務網關接口。是web服務和web應用程序之間的接口規(guī)范,在PEP3333中提出。

wsgi讓應用程序和web服務之間解耦,應用程序只需要遵守規(guī)范,就可以在各種不同的web服務部署運行。比如上圖中,基于flask/django實現的應用程序可以使用gunicorn部署,也可以使用nginx+uwsgi部署。
ASGI
ASGI(Asynchronous Server Gateway Interface) 異步服務器網關接口。ASGI繼承自wsgi,旨在在具有異步功能的Python Web服務器,框架和應用程序之間提供標準接口。ASGI具有WSGI向后兼容性實現以及多個服務器和應用程序框架。

wsgi中使用請求響應模型,每個請求可以同步獲得一個響應。在ASGI中,請求的響應變成異步實現,一般用于websocket協(xié)議。(asgi的內容,涉及異步實現,本文就不多介紹)
cgi 示例
單純的概念理解比較難。下面我們配合示例一起來學習,先從CGI開始。
http 模塊提供了一個簡單的文件目錄服務:
python3 -m http.server Serving HTTP on :: port 8000 (http://[::]:8000/) ...
這個服務只有靜態(tài)的展示功能,我們可以利用cgi擴展一個動態(tài)功能。
cgi腳本
創(chuàng)建cgi-bin目錄,這是CGI中約定的目錄名稱。然后編寫 hello.py, 代碼如下:
#!/usr/bin/env python
import time
import sqlite3
import os
DB_FILE = "guests.db"
def init_db():
pass # 詳情請見附件
def update_total(ts):
pass # 詳情請見附件
print('<html>')
print('<head>')
print('<meta charset="utf-8">')
print('<title>Hello Word!</title>')
print('</head>')
print('<body>')
print('<h2>Hello Python!</h2>')
if not os.path.exists(DB_FILE):
init_db()
total = update_total(time.time())
print(f'total guest: {total}!')
print('</body>')
print('</html>')
為了代碼簡潔,省略了db操作部分的具體實現。還需要給腳本可執(zhí)行權限:
源碼在這里
chmod 755 hello.py
./hello.py <html> <head> <meta charset="utf-8"> <title>Hello Word!</title> </head> <body> <h2>Hello Python!</h2> total guest: 4! </body> </html>
啟動http.server中的cgi服務:
python -m http.server --cgi
注意后面的 --cgi 參數,讓服務使用cgi-handler。啟動后使用 curl 訪問:
curl -v http://127.0.0.1:8000/cgi-bin/hello.py * Trying 127.0.0.1... * TCP_NODELAY set * Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0) > GET /cgi-bin/hello.py HTTP/1.1 > Host: 127.0.0.1:8000 > User-Agent: curl/7.64.1 > Accept: */* > * HTTP 1.0, assume close after body < HTTP/1.0 200 Script output follows < Server: SimpleHTTP/0.6 Python/3.8.5 < Date: Sun, 31 Jan 2021 13:09:29 GMT < <html> < <head> < <meta charset="utf-8"> < <title>Hello Word!</title> < </head> < <body> < <h2>Hello Python!</h2> < total guest: 5! # 訪客數 < </body> < </html> * Closing connection 0
可以看到 hello.py 正確執(zhí)行,訪客數+1。因為數據存儲在db中,重啟服務仍然有效。
cgi服務實現
cgi的實現,主要就是下面的代碼:
# http.server
class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
def run_cgi(self):
import subprocess
cmdline = [scriptfile]
if self.is_python(scriptfile):
interp = sys.executable
cmdline = [interp, '-u'] + cmdline
if '=' not in query:
cmdline.append(query)
try:
nbytes = int(length)
except (TypeError, ValueError):
nbytes = 0
p = subprocess.Popen(cmdline,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env = env
)
if self.command.lower() == "post" and nbytes > 0:
data = self.rfile.read(nbytes)
# throw away additional data [see bug #427345]
while select.select([self.rfile._sock], [], [], 0)[0]:
if not self.rfile._sock.recv(1):
break
stdout, stderr = p.communicate(data)
self.wfile.write(stdout)
p.stderr.close()
p.stdout.close()
status = p.returncode
可見cgi的實現就是:
- 使用subprocess.Popen新開了一個進程去執(zhí)行腳本
- 重定向腳本的輸出到當前socket的wfile,也就是http請求的返回上
代碼也驗證了為什么需要授予 hello.py 的可執(zhí)行權限。
從例子可以了解到http.server專注于提供http服務,app.py專注于業(yè)務功能,兩者通過cgi進行銜接。
wsgiref
wsgiref是python自帶的wsgi的實現參考(reference), 主要代碼結構:
| 文件 | 描述 |
|---|---|
| handlers.py | wsgi實現 |
| headers.py | 管理http-header |
| simple_server.py | 支持wsgi的http服務 |
| util.py&&validator.py | 工具和驗證器 |
WSGIServer的代碼:
class WSGIServer(HTTPServer):
"""BaseHTTPServer that implements the Python WSGI protocol"""
application = None
def server_bind(self):
"""Override server_bind to store the server name."""
HTTPServer.server_bind(self)
self.setup_environ()
def setup_environ(self): # 初始化環(huán)境變量
# Set up base environment
env = self.base_environ = {}
env['SERVER_NAME'] = self.server_name
env['GATEWAY_INTERFACE'] = 'CGI/1.1'
env['SERVER_PORT'] = str(self.server_port)
env['REMOTE_HOST']=''
env['CONTENT_LENGTH']=''
env['SCRIPT_NAME'] = ''
def get_app(self):
return self.application
def set_app(self,application): # 注入application的class,注意是class
self.application = application
WSGIServer并不復雜,繼承自http-server,接受application注入,就把web-server和we-application銜接起來。銜接后的動作,則是老規(guī)矩,交給HTTPRequestHandler去實現。同時wsgi服務多了一個準備env的動作,約定了一些wsgi的環(huán)境變量。
class WSGIRequestHandler(BaseHTTPRequestHandler): server_version = "WSGIServer/" + __version__ def get_environ(self): pass def handle(self): """Handle a single HTTP request""" self.raw_requestline = self.rfile.readline(65537) if len(self.raw_requestline) > 65536: ... self.send_error(414) return if not self.parse_request(): # An error code has been sent, just exit return handler = ServerHandler( self.rfile, self.wfile, self.get_stderr(), self.get_environ(), multithread=False, ) # 創(chuàng)建新的業(yè)務handler handler.request_handler = self handler.run(self.server.get_app()) # 創(chuàng)建application對象
WSGIRequestHandler覆蓋了handler,處理完成http協(xié)議(parse_request)后, 又做了四個動作:
- 創(chuàng)建environ
- 創(chuàng)建ServerHandler對象
- 創(chuàng)建app對象
- 運行app
environ處理主要是把http請求的header信息附帶在wsgi-server的環(huán)境變量上:
def get_environ(self):
env = self.server.base_environ.copy() # wsgi-server的環(huán)境變量
env['SERVER_PROTOCOL'] = self.request_version
env['SERVER_SOFTWARE'] = self.server_version
env['REQUEST_METHOD'] = self.command
...
host = self.address_string()
if host != self.client_address[0]:
env['REMOTE_HOST'] = host
env['REMOTE_ADDR'] = self.client_address[0]
if self.headers.get('content-type') is None:
env['CONTENT_TYPE'] = self.headers.get_content_type()
else:
env['CONTENT_TYPE'] = self.headers['content-type']
length = self.headers.get('content-length')
if length:
env['CONTENT_LENGTH'] = length
for k, v in self.headers.items():
k=k.replace('-','_').upper(); v=v.strip()
if k in env:
continue # skip content length, type,etc.
if 'HTTP_'+k in env:
env['HTTP_'+k] += ','+v # comma-separate multiple headers
else:
env['HTTP_'+k] = v
return env
ServerHandler對象的創(chuàng)建,接受輸入/輸出/錯誤,以及環(huán)境變量信息:
class ServerHandler(BaseHandler): def __init__(self,stdin,stdout,stderr,environ, multithread=True, multiprocess=False ): self.stdin = stdin self.stdout = stdout self.stderr = stderr self.base_env = environ self.wsgi_multithread = multithread self.wsgi_multiprocess = multiprocess ...
重點在ServerHandler的run函數:
class BaseHandler: def run(self, application): """Invoke the application""" # Note to self: don't move the close()! Asynchronous servers shouldn't # call close() from finish_response(), so if you close() anywhere but # the double-error branch here, you'll break asynchronous servers by # prematurely closing. Async servers must return from 'run()' without # closing if there might still be output to iterate over. ... self.setup_environ() self.result = application(self.environ, self.start_response) self.finish_response() ...
關鍵的3個步驟:
- setup_environ 繼續(xù)構建環(huán)境變量
- 接受application處理http請求的返回
- 完成http響應
setup_environ對env進行了進一步的包裝,附帶了請求的in/error,這樣讓使用env就可以對http請求進行讀寫。
def setup_environ(self):
"""Set up the environment for one request"""
env = self.environ = self.os_environ.copy()
self.add_cgi_vars() # 子類實現 self.environ.update(self.base_env)
env['wsgi.input'] = self.get_stdin() # 注意沒有stdout
env['wsgi.errors'] = self.get_stderr()
env['wsgi.version'] = self.wsgi_version
env['wsgi.run_once'] = self.wsgi_run_once
env['wsgi.url_scheme'] = self.get_scheme()
env['wsgi.multithread'] = self.wsgi_multithread
env['wsgi.multiprocess'] = self.wsgi_multiprocess
if self.wsgi_file_wrapper is not None:
env['wsgi.file_wrapper'] = self.wsgi_file_wrapper
if self.origin_server and self.server_software:
env.setdefault('SERVER_SOFTWARE',self.server_software)
env的處理過程,可以理解成3步:1)附加server的運行信息 2)附加請求的http頭(協(xié)議信息) 3)附加請求的流信息。env,可以換個說法就是http請求的所有上下文環(huán)境。
application還接收一個回調函數start_response,主要是按照http協(xié)議的規(guī)范,生成響應狀態(tài)和response_header:
def start_response(self, status, headers,exc_info=None): """'start_response()' callable as specified by PEP 3333""" self.status = status self.headers = self.headers_class(headers) status = self._convert_string_type(status, "Status") assert len(status)>=4,"Status must be at least 4 characters" assert status[:3].isdigit(), "Status message must begin w/3-digit code" assert status[3]==" ", "Status message must have a space after code" return self.write
application對請求的處理:
def demo_app(environ,start_response):
from io import StringIO
stdout = StringIO()
print("Hello world!", file=stdout)
print(file=stdout)
# http請求及環(huán)境
h = sorted(environ.items())
for k,v in h:
print(k,'=',repr(v), file=stdout)
# 回調寫入http_status, response_headers
start_response("200 OK", [('Content-Type','text/plain; charset=utf-8')])
# 返回處理結果response_body
return [stdout.getvalue().encode("utf-8")]
響應仍然由ServerHandler寫入:
def finish_response(self): if not self.result_is_file() or not self.sendfile(): for data in self.result: self.write(data) self.finish_content()
可以使用下面命令測試這個流程:
python -m wsgiref.simple_server Serving HTTP on 0.0.0.0 port 8000 ... 127.0.0.1 - - [31/Jan/2021 21:43:05] "GET /xyz?abc HTTP/1.1" 200 3338
wsgi 小結
簡單小結wsgi的實現。在http請求的處理流程web-browser <-> web-server <-> wsgi <-> web-application中,體現了分層的思想,每層做不同的事情:
- web-server處理http/tcp協(xié)議,線程/進程的調度等底層實現
- wsgi承上啟下,接受http請求,調用applicaiton處理請求,完成響應
- application處理上層業(yè)務邏輯
小技巧
在wsgiref代碼中一樣有各種小的技巧, 學習后可以讓我們的代碼更pythonic。
環(huán)境變量都這樣設置:
def setup_environ(self):
# Set up base environment
env = self.base_environ = {}
env['SERVER_NAME'] = self.server_name
env['GATEWAY_INTERFACE'] = 'CGI/1.1'
...
我之前大概都是這樣寫:
def setup_environ(self):
self.base_environ = {}
self.base_environ['SERVER_NAME'] = self.server_name
self.base_environ['GATEWAY_INTERFACE'] = 'CGI/1.1'
對比后,可以發(fā)現前面的寫法更簡潔一些。
比如流的持續(xù)寫入:
def _write(self,data):
result = self.stdout.write(data)
if result is None or result == len(data):
return
from warnings import warn
warn("SimpleHandler.stdout.write() should not do partial writes",
DeprecationWarning)
while True:
data = data[result:] # 持續(xù)的寫入,直到完成
if not data:
break
result = self.stdout.write(data)
比如header的處理,實際上是把數組當作字典使用:
class Headers:
"""Manage a collection of HTTP response headers"""
def __init__(self, headers=None):
headers = headers if headers is not None else []
self._headers = headers # 內部存儲使用數組
def __setitem__(self, name, val):
"""Set the value of a header."""
del self[name]
self._headers.append(
(self._convert_string_type(name), self._convert_string_type(val)))
....
def __getitem__(self,name):
"""Get the first header value for 'name'
Return None if the header is missing instead of raising an exception.
Note that if the header appeared multiple times, the first exactly which
occurrence gets returned is undefined. Use getall() to get all
the values matching a header field name.
"""
return self.get(name)
def get(self,name,default=None):
"""Get the first header value for 'name', or return 'default'"""
name = self._convert_string_type(name.lower())
for k,v in self._headers:
if k.lower()==name:
return v
return default
這樣對 Content-Type: application/javascript; charset=utf-8 這樣的值,可以使用下面方式使用:
if self.headers.get('content-type') is None:
env['CONTENT_TYPE'] = self.headers.get_content_type()
else:
env['CONTENT_TYPE'] = self.headers['content-type']
為什么用數組,而不是用字典呢?我猜測是因為header的特性是數據多為讀操作。
以上就是python wsgiref源碼解析的詳細內容,更多關于python wsgiref源碼的資料請關注腳本之家其它相關文章!
- Python WSGI 規(guī)范簡介
- 淺析Python 中的 WSGI 接口和 WSGI 服務的運行
- Docker構建python Flask+ nginx+uwsgi容器
- python 解決flask uwsgi 獲取不到全局變量的問題
- python web框架 django wsgi原理解析
- VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法詳解
- Python開發(fā)之Nginx+uWSGI+virtualenv多項目部署教程
- CentOS7部署Flask(Apache、mod_wsgi、Python36、venv)
- 詳解如何在Apache中運行Python WSGI應用
- python 內置庫wsgiref的使用(WSGI基礎入門)
相關文章
詳解解決Python memory error的問題(四種解決方案)
這篇文章主要介紹了詳解解決Python memory error的問題(四種解決方案),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08
Python壓縮包處理模塊zipfile和py7zr操作代碼
目前對文件的壓縮和解壓縮比較常用的格式就是zip格式和7z格式,這篇文章主要介紹了Python壓縮包處理模塊zipfile和py7zr,需要的朋友可以參考下2022-06-06

