文件 | 描述 |
---|---|
handlers.py | wsgi实现 |
headers.py | 管理http-header |
simple_server.py | 支持wsgi的http服务 |
util.pyvalidator.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): # 初始化环境变量 # 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衔接起来。衔接后的动作,则是老规矩,交给HTTPRequestHandler去实现。同时wsgi服务多了一个准备env的动作,约定了一些wsgi的环境变量。
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, ) # 创建新的业务handler handler.request_handler = self handler.run(self.server.get_app()) # 创建application对象
WSGIRequestHandler覆盖了handler,处理完成http协议(parse_request)后, 又做了四个动作:
environ处理主要是把http请求的header信息附带在wsgi-server的环境变量上:
def get_environ(self): env = self.server.base_environ.copy() # wsgi-server的环境变量 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对象的创建,接受输入/输出/错误,以及环境变量信息:
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对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头(协议信息) 3)附加请求的流信息。env,可以换个说法就是http请求的所有上下文环境。
application还接收一个回调函数start_response,主要是按照http协议的规范,生成响应状态和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请求及环境 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的实现。在http请求的处理流程web-browser -> web-server -> wsgi -> web-application中,体现了分层的思想,每层做不同的事情:
在wsgiref代码中一样有各种小的技巧, 学习后可以让我们的代码更pythonic。
环境变量都这样设置:
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'
对比后,可以发现前面的写法更简洁一些。
比如流的持续写入:
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:] # 持续的写入,直到完成 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源码的资料请关注脚本之家其它相关文章!