Skip to content

Instantly share code, notes, and snippets.

@sontek
Created October 21, 2014 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sontek/7ac0a1bc323784cacba8 to your computer and use it in GitHub Desktop.
Save sontek/7ac0a1bc323784cacba8 to your computer and use it in GitHub Desktop.
JessieWeb
from wsgiref.simple_server import make_server
from wsgiref.util import application_uri
import sys
import json
def home():
return 'hello world'
def api():
return {'abc': 123}
def five_hundred():
return 'unhandled error!'
def four_oh_four():
return 'Not Found!'
def application(environ, start_response):
path = environ['PATH_INFO']
try:
status = '200 OK'
html_routes = ['/']
api_routes = ['/api']
if path in html_routes:
headers = [('Content-type', 'text/html')]
result = home()
elif path in api_routes:
headers = [('Content-type', 'application/json')]
result = json.dumps(api())
else:
status = '404 Not Found'
headers = [('Content-type', 'text/plain')]
result = four_oh_four()
except Exception as e:
status = '500 Internal Server Error'
result = five_hundred()
start_response(str(status), headers)
return result
if __name__ == '__main__':
print("Serving on http://localhost:8080")
httpd = make_server('', 8080, application)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment