Created
April 26, 2017 10:38
-
-
Save antoni/cffd1305c7767312791bbca0c1e7f315 to your computer and use it in GitHub Desktop.
JSON server, which serves local directory static files according to file's basename.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
JSON server, which serves local directory static files according to file's basename. | |
echo '{"key": "valuy"}' > resp.json | |
python -m JsonServer | |
curl http://localhost:8000/resp | |
""" | |
import os.path | |
import sys | |
from SimpleHTTPServer import SimpleHTTPRequestHandler, test | |
try: | |
from cStringIO import StringIO | |
except ImportError: | |
from StringIO import StringIO | |
class JsonServer(SimpleHTTPRequestHandler): | |
def _set_headers(self): | |
self.send_response(200) | |
self.send_header('Content-type', 'application/json') | |
self.end_headers() | |
def do_GET(self): | |
print self.path | |
self.path += '.json' | |
if not os.path.isfile('./' + self.path): | |
self.send_error(404, 'File Not Found: %s' % self.path) | |
else: | |
return SimpleHTTPRequestHandler.do_GET(self) | |
if __name__ == "__main__": | |
test(HandlerClass=JsonServer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment