Skip to content

Instantly share code, notes, and snippets.

@amyangfei
Last active August 29, 2015 14:12
Show Gist options
  • Save amyangfei/13b820b7da7767d04402 to your computer and use it in GitHub Desktop.
Save amyangfei/13b820b7da7767d04402 to your computer and use it in GitHub Desktop.
from wsgiref.simple_server import make_server
class AppClass:
def __call__(self,environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
return ["hello world!"]
app = AppClass()
httpd = make_server('', 8000, app)
print "Serving on port 8000..."
httpd.serve_forever()
from wsgiref.simple_server import make_server
URL_PATTERNS= (
('hi/','say_hi'),
('hello/','say_hello'),
)
class Dispatcher(object):
def _match(self,path):
path = path.split('/')[1]
for url,app in URL_PATTERNS:
if path in url:
return app
def __call__(self, environ, start_response):
path = environ.get('PATH_INFO','/')
app = self._match(path)
if app :
app = globals()[app]
return app(environ, start_response)
else:
start_response("404 NOT FOUND",[('Content-type', 'text/plain')])
return ["Page not found!"]
def say_hi(environ, start_response):
start_response("200 OK",[('Content-type', 'text/html')])
return ["wsgi say hi to you!"]
def say_hello(environ, start_response):
start_response("200 OK",[('Content-type', 'text/html')])
return ["wsgi say hello to you!"]
app = Dispatcher()
httpd = make_server('', 8000, app)
print "Serving on port 8000..."
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment