下記のようにデバックしていて、気になりました。引数のenviron, start_responseは、どのモジュールのどこの記述箇所でセットされているのか
start_responseの定義がある箇所はわかっています。
python
1from wsgiref.util import setup_testing_defaults 2from wsgiref.simple_server import make_server 3 4# A relatively simple WSGI application. It's going to print out the 5# environment dictionary after being updated by setup_testing_defaults 6def simple_app(environ, start_response): 7 import pdb;pdb.set_trace() 8 print(environ) 9 setup_testing_defaults(environ) 10 print(environ) 11 print(start_response) 12 status = '200 OK' 13 headers = [('Content-type', 'text/plain; charset=utf-8')] 14 15 start_response(status, headers) 16 17 ret = [("%s: %s\n" % (key, value)).encode("utf-8") 18 for key, value in environ.items()] 19 return ret 20 21with make_server('', 8000, simple_app) as httpd: 22 print("Serving on port 8000...") 23 httpd.serve_forever() 24
デバッカーでの確認結果
python
1bound method BaseHandler.start_response of <wsgiref.simple_server.ServerHandler object
各関数の定義の場所
text
1start_responseの定義の場所 2wsgiref > handers.py > BaseHandler > def start_response 3 4make_serverの定義の場所 5wsgiref > simple_server.py > def make_server
start_responseの定義
python
1def start_response(self, status, headers,exc_info=None): 2 """'start_response()' callable as specified by PEP 3333""" 3 4 if exc_info: 5 try: 6 if self.headers_sent: 7 # Re-raise original exception if headers sent 8 raise exc_info[0](exc_info[1]).with_traceback(exc_info[2]) 9 finally: 10 exc_info = None # avoid dangling circular ref 11 elif self.headers is not None: 12 raise AssertionError("Headers already set!") 13 14 self.status = status 15 self.headers = self.headers_class(headers) 16 status = self._convert_string_type(status, "Status") 17 assert len(status)>=4,"Status must be at least 4 characters" 18 assert status[:3].isdigit(), "Status message must begin w/3-digit code" 19 assert status[3]==" ", "Status message must have a space after code" 20 21 if __debug__: 22 for name, val in headers: 23 name = self._convert_string_type(name, "Header name") 24 val = self._convert_string_type(val, "Header value") 25 assert not is_hop_by_hop(name),\ 26 f"Hop-by-hop header, '{name}: {val}', not allowed" 27 28 return self.write
make_serverの定義
python
1def make_server( 2 host, port, app, server_class=WSGIServer, handler_class=WSGIRequestHandler 3): 4 """Create a new WSGI server listening on `host` and `port` for `app`""" 5 server = server_class((host, port), handler_class) 6 server.set_app(app) 7 return server
set_appの定義の箇所
python
1class WSGIServer(HTTPServer): 2 3 """BaseHTTPServer that implements the Python WSGI protocol""" 4 5 application = None 6 7 def server_bind(self): 8 """Override server_bind to store the server name.""" 9 HTTPServer.server_bind(self) 10 self.setup_environ() 11 12 def setup_environ(self): 13 # Set up base environment 14 env = self.base_environ = {} 15 env['SERVER_NAME'] = self.server_name 16 env['GATEWAY_INTERFACE'] = 'CGI/1.1' 17 env['SERVER_PORT'] = str(self.server_port) 18 env['REMOTE_HOST']='' 19 env['CONTENT_LENGTH']='' 20 env['SCRIPT_NAME'] = '' 21 22 def get_app(self): 23 return self.application 24 25 def set_app(self,application): 26 self.application = application
uwsgiの場合は、setup_pypy.pyに定義があり、同じ読み込み方をしているのかどうかは気になります。
該当コード
関連する質問はこちら
回答2件
あなたの回答
tips
プレビュー