Create a gist now

Instantly share code, notes, and snippets.

Simple WSGI server and app
import socket
# see wsgiref.simple_server for a real implementation of a WSGI server
def serve(app):
listener = socket.socket()
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listener.bind(('', 8080))
listener.listen(5)
while True:
s, addr = listener.accept()
print 'server received connection from', addr
request = s.recv(10000)
print 'request we received:', request
method, rest = request.split(' ', 1)
path, rest = rest.split(None, 1)
def start_response(status, headers):
print 'sending headers'
stuff = '\r\n'.join(['HTTP/0.9 '+status] + [k+': '+v for k, v in headers])
print stuff
s.send(stuff)
s.send('\r\n\r\n')
environ = {'REQUEST_METHOD': method, "PATH_INFO":path}
for data in app(environ, start_response):
print 'sending data'
s.send(data)
s.close()
def demo_app(environ, start_response):
start_response("200 OK", [('Content-Type','text/plain')])
return [('You asked to '+environ['REQUEST_METHOD']+' '+environ['PATH_INFO']), 'and that is what we just did!']
if __name__ == '__main__':
serve(demo_app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment