Created
June 14, 2015 14:04
-
-
Save sahilshekhawat/a2e79a8df8ce4f31ff4e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 sys | |
import signal | |
import webbrowser | |
import BaseHTTPServer | |
from SimpleHTTPServer import SimpleHTTPRequestHandler | |
class Server(object): | |
def __init__(self, port=8000, scene_file="Null", directory="static/"): | |
self.port = port | |
self.scene_file = scene_file | |
self.directory = directory | |
self.keep_running = True | |
def run_server(self): | |
os.chdir(self.directory) | |
HandlerClass = SimpleHTTPRequestHandler | |
ServerClass = BaseHTTPServer.HTTPServer | |
Protocol = "HTTP/1.0" | |
server_address = ('127.0.0.1', self.port) | |
HandlerClass.protocol_version = Protocol | |
self.httpd = ServerClass(server_address, HandlerClass) | |
sa = self.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="+\ | |
self.scene_file | |
print url | |
webbrowser.open(url) | |
signal.signal(signal.SIGINT, self.kill_server_handler) | |
self.httpd.socket.settimeout(0.01) | |
while self.keep_running: | |
self.httpd.handle_request() | |
def kill_server_handler(self, signal, frame): | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment