1 | import random
|
---|
2 | import socket
|
---|
3 | import string
|
---|
4 | from wsgiref import simple_server
|
---|
5 | from wsgiref.util import setup_testing_defaults
|
---|
6 |
|
---|
7 |
|
---|
8 | class ServerHandler(simple_server.ServerHandler):
|
---|
9 | http_version = '1.1'
|
---|
10 |
|
---|
11 |
|
---|
12 | class WSGIRequestHandler(simple_server.WSGIRequestHandler):
|
---|
13 | protocol_version = 'HTTP/1.1'
|
---|
14 |
|
---|
15 | def handle(self):
|
---|
16 | """Copy of WSGIRequestHandler.handle() but with different ServerHandler"""
|
---|
17 | self.raw_requestline = self.rfile.readline(65537)
|
---|
18 | if len(self.raw_requestline) > 65536:
|
---|
19 | self.requestline = ''
|
---|
20 | self.request_version = ''
|
---|
21 | self.command = ''
|
---|
22 | self.send_error(414)
|
---|
23 | return
|
---|
24 |
|
---|
25 | if not self.parse_request(): # An error code has been sent, just exit
|
---|
26 | return
|
---|
27 |
|
---|
28 | handler = ServerHandler(
|
---|
29 | self.rfile, self.wfile, self.get_stderr(), self.get_environ()
|
---|
30 | )
|
---|
31 | handler.request_handler = self # backpointer for logging & connection closing
|
---|
32 | handler.run(self.server.get_app())
|
---|
33 |
|
---|
34 |
|
---|
35 | def large_response(environ, start_response):
|
---|
36 | setup_testing_defaults(environ)
|
---|
37 |
|
---|
38 | status = '200 OK'
|
---|
39 | headers = [('Content-type', 'text/plain; charset=utf-8')]
|
---|
40 |
|
---|
41 | start_response(status, headers)
|
---|
42 |
|
---|
43 | response_body = ''.join(random.choices(string.ascii_uppercase + string.digits, k=1024*1024))
|
---|
44 |
|
---|
45 | return [response_body.encode()]
|
---|
46 |
|
---|
47 |
|
---|
48 | with simple_server.make_server('0.0.0.0', 8001, large_response, handler_class=WSGIRequestHandler) as httpd:
|
---|
49 | print("Serving on port 8001...")
|
---|
50 | httpd.serve_forever()
|
---|