Skip to content

Instantly share code, notes, and snippets.

@sahilshekhawat
Created June 14, 2015 14:10
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 sahilshekhawat/93d333af699b82c8bda7 to your computer and use it in GitHub Desktop.
Save sahilshekhawat/93d333af699b82c8bda7 to your computer and use it in GitHub Desktop.
#!/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