Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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))
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
environ = {'REQUEST_METHOD': method, "PATH_INFO":path}
for data in app(environ, start_response):
print 'sending data'
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__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment