replaced Handler with class

This commit is contained in:
Abubakar Abid 2019-02-24 22:35:11 -08:00
parent 0b76b9e2c8
commit e434694ada
3 changed files with 46 additions and 28 deletions

View File

@ -59,14 +59,14 @@
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [24/Feb/2019 22:21:36] \"GET /interface.html HTTP/1.1\" 200 -\n",
"127.0.0.1 - - [24/Feb/2019 22:21:36] \"GET /js/all-io.js HTTP/1.1\" 200 -\n",
"127.0.0.1 - - [24/Feb/2019 22:21:36] \"GET /css/bootstrap.min.css HTTP/1.1\" 200 -\n",
"127.0.0.1 - - [24/Feb/2019 22:21:37] \"GET /css/draw-a-digit.css HTTP/1.1\" 200 -\n",
"127.0.0.1 - - [24/Feb/2019 22:21:37] \"GET /js/bootstrap.min.js HTTP/1.1\" 200 -\n",
"127.0.0.1 - - [24/Feb/2019 22:21:37] \"GET /js/bootstrap-notify.min.js HTTP/1.1\" 200 -\n",
"127.0.0.1 - - [24/Feb/2019 22:21:37] \"GET /js/textbox-output.js HTTP/1.1\" 200 -\n",
"127.0.0.1 - - [24/Feb/2019 22:21:37] \"GET /js/textbox-input.js HTTP/1.1\" 200 -\n"
"127.0.0.1 - - [24/Feb/2019 22:25:21] \"GET /interface.html HTTP/1.1\" 200 -\n",
"127.0.0.1 - - [24/Feb/2019 22:25:21] \"GET /js/all-io.js HTTP/1.1\" 200 -\n",
"127.0.0.1 - - [24/Feb/2019 22:25:21] \"GET /css/bootstrap.min.css HTTP/1.1\" 200 -\n",
"127.0.0.1 - - [24/Feb/2019 22:25:21] \"GET /css/draw-a-digit.css HTTP/1.1\" 200 -\n",
"127.0.0.1 - - [24/Feb/2019 22:25:21] \"GET /js/bootstrap.min.js HTTP/1.1\" 200 -\n",
"127.0.0.1 - - [24/Feb/2019 22:25:21] \"GET /js/bootstrap-notify.min.js HTTP/1.1\" 200 -\n",
"127.0.0.1 - - [24/Feb/2019 22:25:21] \"GET /js/textbox-input.js HTTP/1.1\" 200 -\n",
"127.0.0.1 - - [24/Feb/2019 22:25:21] \"GET /js/textbox-output.js HTTP/1.1\" 200 -\n"
]
}
],

View File

@ -178,7 +178,7 @@ class Interface():
start_server = websockets.serve(self.communicate, LOCALHOST_IP, INITIAL_WEBSOCKET_PORT + i)
self._set_socket_port_in_js(output_directory, INITIAL_WEBSOCKET_PORT + i)
if verbose:
print("NOTE: Gradio is in beta2 stage, please report all bugs to: a12d@stanford.edu")
print("NOTE: Gradio is in beta stage, please report all bugs to: a12d@stanford.edu")
print("Model available locally at: {}".format(path_to_server + TEMPLATE_TEMP))
if share_link:

View File

@ -11,6 +11,7 @@ import signal
import http.server
import socketserver
import threading
from http.server import HTTPServer as BaseHTTPServer, SimpleHTTPRequestHandler
INITIAL_PORT_VALUE = 7860
TRY_NUM_PORTS = 100
@ -47,34 +48,51 @@ def get_ports_in_use(start, stop):
def serve_files_in_background(port, directory_to_serve=None):
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=directory_to_serve, **kwargs)
# class Handler(http.server.SimpleHTTPRequestHandler):
# def __init__(self, *args, **kwargs):
# super().__init__(*args, directory=directory_to_serve, **kwargs)
#
# server = socketserver.ThreadingTCPServer(('localhost', port), Handler)
# # Ensures that Ctrl-C cleanly kills all spawned threads
# server.daemon_threads = True
# # Quicker rebinding
# server.allow_reuse_address = True
#
# # A custom signal handle to allow us to Ctrl-C out of the process
# def signal_handler(signal, frame):
# print('Exiting http server (Ctrl+C pressed)')
# try:
# if (server):
# server.server_close()
# finally:
# sys.exit(0)
#
# # Install the keyboard interrupt handler
# signal.signal(signal.SIGINT, signal_handler)
class HTTPHandler(SimpleHTTPRequestHandler):
"""This handler uses server.base_path instead of always using os.getcwd()"""
server = socketserver.ThreadingTCPServer(('localhost', port), Handler)
# Ensures that Ctrl-C cleanly kills all spawned threads
server.daemon_threads = True
# Quicker rebinding
server.allow_reuse_address = True
def translate_path(self, path):
path = SimpleHTTPRequestHandler.translate_path(self, path)
relpath = os.path.relpath(path, os.getcwd())
fullpath = os.path.join(self.server.base_path, relpath)
return fullpath
# A custom signal handle to allow us to Ctrl-C out of the process
def signal_handler(signal, frame):
print('Exiting http server (Ctrl+C pressed)')
try:
if (server):
server.server_close()
finally:
sys.exit(0)
class HTTPServer(BaseHTTPServer):
"""The main server, you pass in base_path which is the path you want to serve requests from"""
# Install the keyboard interrupt handler
signal.signal(signal.SIGINT, signal_handler)
def __init__(self, base_path, server_address, RequestHandlerClass=HTTPHandler):
self.base_path = base_path
BaseHTTPServer.__init__(self, server_address, RequestHandlerClass)
httpd = HTTPServer(directory_to_serve, (LOCALHOST_NAME, port))
# Now loop forever
def serve_forever():
try:
while True:
sys.stdout.flush()
server.serve_forever()
httpd.serve_forever()
except KeyboardInterrupt:
pass