文件 | 描述 |
---|---|
handlers.py | wsgi實(shí)現(xiàn) |
headers.py | 管理http-header |
simple_server.py | 支持wsgi的http服務(wù) |
util.pyvalidator.py | 工具和驗(yàn)證器 |
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并不復(fù)雜,繼承自http-server,接受application注入,就把web-server和we-application銜接起來。銜接后的動(dòng)作,則是老規(guī)矩,交給HTTPRequestHandler去實(shí)現(xiàn)。同時(shí)wsgi服務(wù)多了一個(gè)準(zhǔn)備env的動(dòng)作,約定了一些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è)務(wù)handler handler.request_handler = self handler.run(self.server.get_app()) # 創(chuàng)建application對象
WSGIRequestHandler覆蓋了handler,處理完成http協(xié)議(parse_request)后, 又做了四個(gè)動(dòng)作:
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)建,接受輸入/輸出/錯(cuò)誤,以及環(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 ...
重點(diǎn)在ServerHandler的run函數(shù):
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() ...
關(guān)鍵的3個(gè)步驟:
setup_environ對env進(jìn)行了進(jìn)一步的包裝,附帶了請求的in/error,這樣讓使用env就可以對http請求進(jìn)行讀寫。
def setup_environ(self): """Set up the environment for one request""" env = self.environ = self.os_environ.copy() self.add_cgi_vars() # 子類實(shí)現(xiàn) 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的運(yùn)行信息 2)附加請求的http頭(協(xié)議信息) 3)附加請求的流信息。env,可以換個(gè)說法就是http請求的所有上下文環(huán)境。
application還接收一個(gè)回調(diào)函數(shù)start_response,主要是按照http協(xié)議的規(guī)范,生成響應(yīng)狀態(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) # 回調(diào)寫入http_status, response_headers start_response("200 OK", [('Content-Type','text/plain; charset=utf-8')]) # 返回處理結(jié)果response_body return [stdout.getvalue().encode("utf-8")]
響應(yīng)仍然由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()
可以使用下面命令測試這個(gè)流程:
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
簡單小結(jié)wsgi的實(shí)現(xiàn)。在http請求的處理流程web-browser -> web-server -> wsgi -> web-application中,體現(xiàn)了分層的思想,每層做不同的事情:
在wsgiref代碼中一樣有各種小的技巧, 學(xué)習(xí)后可以讓我們的代碼更pythonic。
環(huán)境變量都這樣設(shè)置:
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ā)現(xiàn)前面的寫法更簡潔一些。
比如流的持續(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的處理,實(shí)際上是把數(shù)組當(dāng)作字典使用:
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 # 內(nèi)部存儲使用數(shù)組 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']
為什么用數(shù)組,而不是用字典呢?我猜測是因?yàn)閔eader的特性是數(shù)據(jù)多為讀操作。
以上就是python wsgiref源碼解析的詳細(xì)內(nèi)容,更多關(guān)于python wsgiref源碼的資料請關(guān)注腳本之家其它相關(guān)文章!
標(biāo)簽:廊坊 臨汾 河池 重慶 長春 漢中 德宏 東莞
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python wsgiref源碼解析》,本文關(guān)鍵詞 python,wsgiref,源碼,解析,python,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。