Created
June 14, 2015 14:10
-
-
Save sahilshekhawat/93d333af699b82c8bda7 to your computer and use it in GitHub Desktop.
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 | |
import os | |
import webbrowser | |
import BaseHTTPServer | |
from SimpleHTTPServer import SimpleHTTPRequestHandler | |
def run_server(port=8000, scene_file="Null", directory="static/"): | |
#change dir to static first. | |
os.chdir(directory) | |
HandlerClass = SimpleHTTPRequestHandler | |
ServerClass = BaseHTTPServer.HTTPServer | |
Protocol = "HTTP/1.0" | |
server_address = ('127.0.0.1', port) | |
HandlerClass.protocol_version = Protocol | |
httpd = ServerClass(server_address, HandlerClass) | |
sa = httpd.socket.getsockname() | |
print "Serving HTTP on", sa[0], "port", sa[1], "..." | |
print "hit ctrl+c to stop the server.." | |
print "To view visualization, open:\n" | |
url = "http://localhost:"+str(sa[1]) + "/index.html?load="+scene_file | |
print url | |
webbrowser.open(url) | |
keep_running = True | |
try: | |
while keep_running: | |
try: | |
httpd.handle_request() | |
except KeyboardInterrupt: | |
# This needs to be fixed | |
stop_server(httpd) | |
except KeyboardInterrupt: | |
stop_server(httpd) | |
def stop_server(httpd): | |
response = raw_input("Do you really want to exit ([y]/n)? ") | |
if response is (None or 'y'): | |
self.keep_running = False | |
self.httpd.socket.close() | |
if __name__ == "__main__": | |
run_server() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment