]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/wsgiref/simple_server.py
1 """BaseHTTPServer that implements the Python WSGI protocol (PEP 333, rev 1.21)
3 This is both an example of how WSGI can be implemented, and a basis for running
4 simple web applications on a local machine, such as might be done when testing
5 or debugging an application. It has not been reviewed for security issues,
6 however, and we strongly recommend that you use a "real" web server for
9 For example usage, see the 'if __name__=="__main__"' block at the end of the
10 module. See also the BaseHTTPServer module docs for other API information.
13 from BaseHTTPServer
import BaseHTTPRequestHandler
, HTTPServer
15 from wsgiref
.handlers
import SimpleHandler
18 __all__
= ['WSGIServer', 'WSGIRequestHandler', 'demo_app', 'make_server']
21 server_version
= "WSGIServer/" + __version__
22 sys_version
= "Python/" + sys
.version
.split()[0]
23 software_version
= server_version
+ ' ' + sys_version
26 class ServerHandler(SimpleHandler
):
28 server_software
= software_version
32 self
.request_handler
.log_request(
33 self
.status
.split(' ',1)[0], self
.bytes_sent
36 SimpleHandler
.close(self
)
40 class WSGIServer(HTTPServer
):
42 """BaseHTTPServer that implements the Python WSGI protocol"""
46 def server_bind(self
):
47 """Override server_bind to store the server name."""
48 HTTPServer
.server_bind(self
)
51 def setup_environ(self
):
52 # Set up base environment
53 env
= self
.base_environ
= {}
54 env
['SERVER_NAME'] = self
.server_name
55 env
['GATEWAY_INTERFACE'] = 'CGI/1.1'
56 env
['SERVER_PORT'] = str(self
.server_port
)
58 env
['CONTENT_LENGTH']=''
59 env
['SCRIPT_NAME'] = ''
62 return self
.application
64 def set_app(self
,application
):
65 self
.application
= application
69 class WSGIRequestHandler(BaseHTTPRequestHandler
):
71 server_version
= "WSGIServer/" + __version__
73 def get_environ(self
):
74 env
= self
.server
.base_environ
.copy()
75 env
['SERVER_PROTOCOL'] = self
.request_version
76 env
['REQUEST_METHOD'] = self
.command
78 path
,query
= self
.path
.split('?',1)
80 path
,query
= self
.path
,''
82 env
['PATH_INFO'] = urllib
.unquote(path
)
83 env
['QUERY_STRING'] = query
85 host
= self
.address_string()
86 if host
!= self
.client_address
[0]:
87 env
['REMOTE_HOST'] = host
88 env
['REMOTE_ADDR'] = self
.client_address
[0]
90 if self
.headers
.typeheader
is None:
91 env
['CONTENT_TYPE'] = self
.headers
.type
93 env
['CONTENT_TYPE'] = self
.headers
.typeheader
95 length
= self
.headers
.getheader('content-length')
97 env
['CONTENT_LENGTH'] = length
99 for h
in self
.headers
.headers
:
101 k
=k
.replace('-','_').upper(); v
=v
.strip()
103 continue # skip content length, type,etc.
105 env
['HTTP_'+k
] += ','+v
# comma-separate multiple headers
110 def get_stderr(self
):
114 """Handle a single HTTP request"""
116 self
.raw_requestline
= self
.rfile
.readline()
117 if not self
.parse_request(): # An error code has been sent, just exit
120 handler
= ServerHandler(
121 self
.rfile
, self
.wfile
, self
.get_stderr(), self
.get_environ()
123 handler
.request_handler
= self
# backpointer for logging
124 handler
.run(self
.server
.get_app())
128 def demo_app(environ
,start_response
):
129 from StringIO
import StringIO
131 print >>stdout
, "Hello world!"
133 h
= environ
.items(); h
.sort()
135 print >>stdout
, k
,'=', repr(v
)
136 start_response("200 OK", [('Content-Type','text/plain')])
137 return [stdout
.getvalue()]
141 host
, port
, app
, server_class
=WSGIServer
, handler_class
=WSGIRequestHandler
143 """Create a new WSGI server listening on `host` and `port` for `app`"""
144 server
= server_class((host
, port
), handler_class
)
149 if __name__
== '__main__':
150 httpd
= make_server('', 8000, demo_app
)
151 sa
= httpd
.socket
.getsockname()
152 print "Serving HTTP on", sa
[0], "port", sa
[1], "..."
154 webbrowser
.open('http://localhost:8000/xyz?abc')
155 httpd
.handle_request() # serve one request, then exit