Created
October 21, 2014 19:40
-
-
Save sontek/7ac0a1bc323784cacba8 to your computer and use it in GitHub Desktop.
JessieWeb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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