Created
September 11, 2014 06:08
-
-
Save xyb/d5d67429b1976f1c35eb to your computer and use it in GitHub Desktop.
a stub http server loading data from current dir
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 | |
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | |
import os | |
PORT_NUMBER = 8080 | |
class HttpStubDirHandler(BaseHTTPRequestHandler): | |
def do_GET(self): | |
mimetype = 'application/json; charset=UTF-8' | |
if self.path == '/': | |
self.path = '/index' | |
try: | |
path = os.path.join(os.curdir, self.path[1:]) | |
f = open(path) | |
self.send_response(200) | |
self.send_header('Content-type', mimetype) | |
self.end_headers() | |
self.wfile.write(f.read()) | |
f.close() | |
except IOError: | |
self.send_error(404, 'File Not Found: %s' % self.path) | |
print 'please setup', path, 'file' | |
def do_POST(self): | |
return self.do_GET() | |
def run(port): | |
try: | |
server = HTTPServer(('', port), HttpStubDirHandler) | |
print 'Started a stub http service on port', port | |
print 'Loading data from', os.curdir, 'dir' | |
server.serve_forever() | |
except KeyboardInterrupt: | |
print '^C received, shutting down the web server' | |
server.socket.close() | |
if __name__ == '__main__': | |
import sys | |
if len(sys.argv) > 1: | |
port = int(sys.argv[1]) | |
run(port) | |
else: | |
print 'Usage: %s <port>' % sys.argv[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment