1 | n/a | """BaseHTTPServer that implements the Python WSGI protocol (PEP 3333) |
---|
2 | n/a | |
---|
3 | n/a | This is both an example of how WSGI can be implemented, and a basis for running |
---|
4 | n/a | simple web applications on a local machine, such as might be done when testing |
---|
5 | n/a | or debugging an application. It has not been reviewed for security issues, |
---|
6 | n/a | however, and we strongly recommend that you use a "real" web server for |
---|
7 | n/a | production use. |
---|
8 | n/a | |
---|
9 | n/a | For example usage, see the 'if __name__=="__main__"' block at the end of the |
---|
10 | n/a | module. See also the BaseHTTPServer module docs for other API information. |
---|
11 | n/a | """ |
---|
12 | n/a | |
---|
13 | n/a | from http.server import BaseHTTPRequestHandler, HTTPServer |
---|
14 | n/a | import sys |
---|
15 | n/a | import urllib.parse |
---|
16 | n/a | from wsgiref.handlers import SimpleHandler |
---|
17 | n/a | from platform import python_implementation |
---|
18 | n/a | |
---|
19 | n/a | __version__ = "0.2" |
---|
20 | n/a | __all__ = ['WSGIServer', 'WSGIRequestHandler', 'demo_app', 'make_server'] |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | server_version = "WSGIServer/" + __version__ |
---|
24 | n/a | sys_version = python_implementation() + "/" + sys.version.split()[0] |
---|
25 | n/a | software_version = server_version + ' ' + sys_version |
---|
26 | n/a | |
---|
27 | n/a | |
---|
28 | n/a | class ServerHandler(SimpleHandler): |
---|
29 | n/a | |
---|
30 | n/a | server_software = software_version |
---|
31 | n/a | |
---|
32 | n/a | def close(self): |
---|
33 | n/a | try: |
---|
34 | n/a | self.request_handler.log_request( |
---|
35 | n/a | self.status.split(' ',1)[0], self.bytes_sent |
---|
36 | n/a | ) |
---|
37 | n/a | finally: |
---|
38 | n/a | SimpleHandler.close(self) |
---|
39 | n/a | |
---|
40 | n/a | |
---|
41 | n/a | |
---|
42 | n/a | class WSGIServer(HTTPServer): |
---|
43 | n/a | |
---|
44 | n/a | """BaseHTTPServer that implements the Python WSGI protocol""" |
---|
45 | n/a | |
---|
46 | n/a | application = None |
---|
47 | n/a | |
---|
48 | n/a | def server_bind(self): |
---|
49 | n/a | """Override server_bind to store the server name.""" |
---|
50 | n/a | HTTPServer.server_bind(self) |
---|
51 | n/a | self.setup_environ() |
---|
52 | n/a | |
---|
53 | n/a | def setup_environ(self): |
---|
54 | n/a | # Set up base environment |
---|
55 | n/a | env = self.base_environ = {} |
---|
56 | n/a | env['SERVER_NAME'] = self.server_name |
---|
57 | n/a | env['GATEWAY_INTERFACE'] = 'CGI/1.1' |
---|
58 | n/a | env['SERVER_PORT'] = str(self.server_port) |
---|
59 | n/a | env['REMOTE_HOST']='' |
---|
60 | n/a | env['CONTENT_LENGTH']='' |
---|
61 | n/a | env['SCRIPT_NAME'] = '' |
---|
62 | n/a | |
---|
63 | n/a | def get_app(self): |
---|
64 | n/a | return self.application |
---|
65 | n/a | |
---|
66 | n/a | def set_app(self,application): |
---|
67 | n/a | self.application = application |
---|
68 | n/a | |
---|
69 | n/a | |
---|
70 | n/a | |
---|
71 | n/a | class WSGIRequestHandler(BaseHTTPRequestHandler): |
---|
72 | n/a | |
---|
73 | n/a | server_version = "WSGIServer/" + __version__ |
---|
74 | n/a | |
---|
75 | n/a | def get_environ(self): |
---|
76 | n/a | env = self.server.base_environ.copy() |
---|
77 | n/a | env['SERVER_PROTOCOL'] = self.request_version |
---|
78 | n/a | env['SERVER_SOFTWARE'] = self.server_version |
---|
79 | n/a | env['REQUEST_METHOD'] = self.command |
---|
80 | n/a | if '?' in self.path: |
---|
81 | n/a | path,query = self.path.split('?',1) |
---|
82 | n/a | else: |
---|
83 | n/a | path,query = self.path,'' |
---|
84 | n/a | |
---|
85 | n/a | env['PATH_INFO'] = urllib.parse.unquote(path, 'iso-8859-1') |
---|
86 | n/a | env['QUERY_STRING'] = query |
---|
87 | n/a | |
---|
88 | n/a | host = self.address_string() |
---|
89 | n/a | if host != self.client_address[0]: |
---|
90 | n/a | env['REMOTE_HOST'] = host |
---|
91 | n/a | env['REMOTE_ADDR'] = self.client_address[0] |
---|
92 | n/a | |
---|
93 | n/a | if self.headers.get('content-type') is None: |
---|
94 | n/a | env['CONTENT_TYPE'] = self.headers.get_content_type() |
---|
95 | n/a | else: |
---|
96 | n/a | env['CONTENT_TYPE'] = self.headers['content-type'] |
---|
97 | n/a | |
---|
98 | n/a | length = self.headers.get('content-length') |
---|
99 | n/a | if length: |
---|
100 | n/a | env['CONTENT_LENGTH'] = length |
---|
101 | n/a | |
---|
102 | n/a | for k, v in self.headers.items(): |
---|
103 | n/a | k=k.replace('-','_').upper(); v=v.strip() |
---|
104 | n/a | if k in env: |
---|
105 | n/a | continue # skip content length, type,etc. |
---|
106 | n/a | if 'HTTP_'+k in env: |
---|
107 | n/a | env['HTTP_'+k] += ','+v # comma-separate multiple headers |
---|
108 | n/a | else: |
---|
109 | n/a | env['HTTP_'+k] = v |
---|
110 | n/a | return env |
---|
111 | n/a | |
---|
112 | n/a | def get_stderr(self): |
---|
113 | n/a | return sys.stderr |
---|
114 | n/a | |
---|
115 | n/a | def handle(self): |
---|
116 | n/a | """Handle a single HTTP request""" |
---|
117 | n/a | |
---|
118 | n/a | self.raw_requestline = self.rfile.readline(65537) |
---|
119 | n/a | if len(self.raw_requestline) > 65536: |
---|
120 | n/a | self.requestline = '' |
---|
121 | n/a | self.request_version = '' |
---|
122 | n/a | self.command = '' |
---|
123 | n/a | self.send_error(414) |
---|
124 | n/a | return |
---|
125 | n/a | |
---|
126 | n/a | if not self.parse_request(): # An error code has been sent, just exit |
---|
127 | n/a | return |
---|
128 | n/a | |
---|
129 | n/a | handler = ServerHandler( |
---|
130 | n/a | self.rfile, self.wfile, self.get_stderr(), self.get_environ() |
---|
131 | n/a | ) |
---|
132 | n/a | handler.request_handler = self # backpointer for logging |
---|
133 | n/a | handler.run(self.server.get_app()) |
---|
134 | n/a | |
---|
135 | n/a | |
---|
136 | n/a | |
---|
137 | n/a | def demo_app(environ,start_response): |
---|
138 | n/a | from io import StringIO |
---|
139 | n/a | stdout = StringIO() |
---|
140 | n/a | print("Hello world!", file=stdout) |
---|
141 | n/a | print(file=stdout) |
---|
142 | n/a | h = sorted(environ.items()) |
---|
143 | n/a | for k,v in h: |
---|
144 | n/a | print(k,'=',repr(v), file=stdout) |
---|
145 | n/a | start_response("200 OK", [('Content-Type','text/plain; charset=utf-8')]) |
---|
146 | n/a | return [stdout.getvalue().encode("utf-8")] |
---|
147 | n/a | |
---|
148 | n/a | |
---|
149 | n/a | def make_server( |
---|
150 | n/a | host, port, app, server_class=WSGIServer, handler_class=WSGIRequestHandler |
---|
151 | n/a | ): |
---|
152 | n/a | """Create a new WSGI server listening on `host` and `port` for `app`""" |
---|
153 | n/a | server = server_class((host, port), handler_class) |
---|
154 | n/a | server.set_app(app) |
---|
155 | n/a | return server |
---|
156 | n/a | |
---|
157 | n/a | |
---|
158 | n/a | if __name__ == '__main__': |
---|
159 | n/a | with make_server('', 8000, demo_app) as httpd: |
---|
160 | n/a | sa = httpd.socket.getsockname() |
---|
161 | n/a | print("Serving HTTP on", sa[0], "port", sa[1], "...") |
---|
162 | n/a | import webbrowser |
---|
163 | n/a | webbrowser.open('http://localhost:8000/xyz?abc') |
---|
164 | n/a | httpd.handle_request() # serve one request, then exit |
---|