openmedialibrary_platform/Darwin/lib/python3.5/wsgiref/simple_server.py

166 lines
5 KiB
Python
Raw Normal View History

2014-09-30 16:15:32 +00:00
"""BaseHTTPServer that implements the Python WSGI protocol (PEP 3333)
2013-10-11 17:28:32 +00:00
This is both an example of how WSGI can be implemented, and a basis for running
simple web applications on a local machine, such as might be done when testing
or debugging an application. It has not been reviewed for security issues,
however, and we strongly recommend that you use a "real" web server for
production use.
For example usage, see the 'if __name__=="__main__"' block at the end of the
module. See also the BaseHTTPServer module docs for other API information.
"""
2014-09-30 16:15:32 +00:00
from http.server import BaseHTTPRequestHandler, HTTPServer
import sys
import urllib.parse
2013-10-11 17:28:32 +00:00
from wsgiref.handlers import SimpleHandler
2014-09-30 16:15:32 +00:00
from platform import python_implementation
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
__version__ = "0.2"
2013-10-11 17:28:32 +00:00
__all__ = ['WSGIServer', 'WSGIRequestHandler', 'demo_app', 'make_server']
server_version = "WSGIServer/" + __version__
2014-09-30 16:15:32 +00:00
sys_version = python_implementation() + "/" + sys.version.split()[0]
2013-10-11 17:28:32 +00:00
software_version = server_version + ' ' + sys_version
class ServerHandler(SimpleHandler):
server_software = software_version
def close(self):
try:
self.request_handler.log_request(
self.status.split(' ',1)[0], self.bytes_sent
)
finally:
SimpleHandler.close(self)
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):
self.application = application
class WSGIRequestHandler(BaseHTTPRequestHandler):
server_version = "WSGIServer/" + __version__
def get_environ(self):
env = self.server.base_environ.copy()
env['SERVER_PROTOCOL'] = self.request_version
2014-09-30 16:15:32 +00:00
env['SERVER_SOFTWARE'] = self.server_version
2013-10-11 17:28:32 +00:00
env['REQUEST_METHOD'] = self.command
if '?' in self.path:
path,query = self.path.split('?',1)
else:
path,query = self.path,''
2014-09-30 16:15:32 +00:00
env['PATH_INFO'] = urllib.parse.unquote_to_bytes(path).decode('iso-8859-1')
2013-10-11 17:28:32 +00:00
env['QUERY_STRING'] = query
host = self.address_string()
if host != self.client_address[0]:
env['REMOTE_HOST'] = host
env['REMOTE_ADDR'] = self.client_address[0]
2014-09-30 16:15:32 +00:00
if self.headers.get('content-type') is None:
env['CONTENT_TYPE'] = self.headers.get_content_type()
2013-10-11 17:28:32 +00:00
else:
2014-09-30 16:15:32 +00:00
env['CONTENT_TYPE'] = self.headers['content-type']
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
length = self.headers.get('content-length')
2013-10-11 17:28:32 +00:00
if length:
env['CONTENT_LENGTH'] = length
2014-09-30 16:15:32 +00:00
for k, v in self.headers.items():
2013-10-11 17:28:32 +00:00
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
def get_stderr(self):
return sys.stderr
def handle(self):
"""Handle a single HTTP request"""
2015-11-25 01:25:01 +00:00
self.raw_requestline = self.rfile.readline(65537)
if len(self.raw_requestline) > 65536:
self.requestline = ''
self.request_version = ''
self.command = ''
self.send_error(414)
return
2013-10-11 17:28:32 +00:00
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()
)
handler.request_handler = self # backpointer for logging
handler.run(self.server.get_app())
def demo_app(environ,start_response):
2014-09-30 16:15:32 +00:00
from io import StringIO
2013-10-11 17:28:32 +00:00
stdout = StringIO()
2014-09-30 16:15:32 +00:00
print("Hello world!", file=stdout)
print(file=stdout)
h = sorted(environ.items())
2013-10-11 17:28:32 +00:00
for k,v in h:
2014-09-30 16:15:32 +00:00
print(k,'=',repr(v), file=stdout)
start_response("200 OK", [('Content-Type','text/plain; charset=utf-8')])
return [stdout.getvalue().encode("utf-8")]
2013-10-11 17:28:32 +00:00
def make_server(
host, port, app, server_class=WSGIServer, handler_class=WSGIRequestHandler
):
"""Create a new WSGI server listening on `host` and `port` for `app`"""
server = server_class((host, port), handler_class)
server.set_app(app)
return server
if __name__ == '__main__':
httpd = make_server('', 8000, demo_app)
sa = httpd.socket.getsockname()
2014-09-30 16:15:32 +00:00
print("Serving HTTP on", sa[0], "port", sa[1], "...")
2013-10-11 17:28:32 +00:00
import webbrowser
webbrowser.open('http://localhost:8000/xyz?abc')
httpd.handle_request() # serve one request, then exit
httpd.server_close()