make WSGIRequestHandler can use an easy way to replace the ServerHandler
This commit is contained in:
parent
e572c7f6db
commit
2ec11a5616
|
@ -347,6 +347,14 @@ request. (E.g., using the :func:`shift_path_info` function from
|
||||||
:func:`make_server` function. Some possibly relevant methods for overriding in
|
:func:`make_server` function. Some possibly relevant methods for overriding in
|
||||||
subclasses:
|
subclasses:
|
||||||
|
|
||||||
|
:class:`WSGIRequestHandler` has the following class variables:
|
||||||
|
|
||||||
|
.. attribute:: server_handler
|
||||||
|
|
||||||
|
.. versionadded:: 3.9
|
||||||
|
|
||||||
|
The handler class to handle the request. This value should be the subclass
|
||||||
|
:class:`wsgiref.handlers.BaseHandler`
|
||||||
|
|
||||||
.. method:: WSGIRequestHandler.get_environ()
|
.. method:: WSGIRequestHandler.get_environ()
|
||||||
|
|
||||||
|
|
|
@ -46,6 +46,9 @@ class MockHandler(WSGIRequestHandler):
|
||||||
def finish(self):
|
def finish(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class MockExceptionHandler(MockHandler):
|
||||||
|
server_handler = dict
|
||||||
|
|
||||||
|
|
||||||
def hello_app(environ,start_response):
|
def hello_app(environ,start_response):
|
||||||
start_response("200 OK", [
|
start_response("200 OK", [
|
||||||
|
@ -80,6 +83,18 @@ def run_amock(app=hello_app, data=b"GET / HTTP/1.0\n\n"):
|
||||||
|
|
||||||
return out.getvalue(), err.getvalue()
|
return out.getvalue(), err.getvalue()
|
||||||
|
|
||||||
|
def run_amock_handler_with_incorrect_server_handler(app=hello_app, data=b"GET / HTTP/1.0\n\n"):
|
||||||
|
server = make_server("", 80, app, MockServer, MockExceptionHandler)
|
||||||
|
inp = BufferedReader(BytesIO(data))
|
||||||
|
out = BytesIO()
|
||||||
|
olderr = sys.stderr
|
||||||
|
err = sys.stderr = StringIO()
|
||||||
|
|
||||||
|
try:
|
||||||
|
server.finish_request((inp, out), ("127.0.0.1",8888))
|
||||||
|
finally:
|
||||||
|
sys.stderr = olderr
|
||||||
|
|
||||||
def compare_generic_iter(make_it,match):
|
def compare_generic_iter(make_it,match):
|
||||||
"""Utility to compare a generic 2.1/2.2+ iterator with an iterable
|
"""Utility to compare a generic 2.1/2.2+ iterator with an iterable
|
||||||
|
|
||||||
|
@ -131,6 +146,9 @@ class IntegrationTests(TestCase):
|
||||||
"\r\n"
|
"\r\n"
|
||||||
"Hello, world!").encode("iso-8859-1")
|
"Hello, world!").encode("iso-8859-1")
|
||||||
)
|
)
|
||||||
|
def test_run_with_incorrect_server_handler(self):
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
run_amock_handler_with_incorrect_server_handler()
|
||||||
|
|
||||||
def test_plain_hello(self):
|
def test_plain_hello(self):
|
||||||
out, err = run_amock()
|
out, err = run_amock()
|
||||||
|
|
|
@ -13,7 +13,7 @@ module. See also the BaseHTTPServer module docs for other API information.
|
||||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||||
import sys
|
import sys
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
from wsgiref.handlers import SimpleHandler
|
from wsgiref.handlers import BaseHandler, SimpleHandler
|
||||||
from platform import python_implementation
|
from platform import python_implementation
|
||||||
|
|
||||||
__version__ = "0.2"
|
__version__ = "0.2"
|
||||||
|
@ -71,6 +71,12 @@ class WSGIServer(HTTPServer):
|
||||||
class WSGIRequestHandler(BaseHTTPRequestHandler):
|
class WSGIRequestHandler(BaseHTTPRequestHandler):
|
||||||
|
|
||||||
server_version = "WSGIServer/" + __version__
|
server_version = "WSGIServer/" + __version__
|
||||||
|
server_handler = ServerHandler
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
if not issubclass(self.server_handler, BaseHandler):
|
||||||
|
raise ValueError("the server_handler should be the subclass of BaseHandler")
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
def get_environ(self):
|
def get_environ(self):
|
||||||
env = self.server.base_environ.copy()
|
env = self.server.base_environ.copy()
|
||||||
|
@ -126,7 +132,7 @@ class WSGIRequestHandler(BaseHTTPRequestHandler):
|
||||||
if not self.parse_request(): # An error code has been sent, just exit
|
if not self.parse_request(): # An error code has been sent, just exit
|
||||||
return
|
return
|
||||||
|
|
||||||
handler = ServerHandler(
|
handler = self.server_handler(
|
||||||
self.rfile, self.wfile, self.get_stderr(), self.get_environ(),
|
self.rfile, self.wfile, self.get_stderr(), self.get_environ(),
|
||||||
multithread=False,
|
multithread=False,
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
make WSGIRequestHandler can use an easy way to replace the ServerHandler
|
Loading…
Reference in New Issue