Created
January 10, 2014 00:37
-
-
Save mikofski/8344871 to your computer and use it in GitHub Desktop.
simple non-blocking asynchronous web server to serve help docs since sphinx search doesn't work on google chrome otherwise
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
# -*- coding: utf-8 -*- | |
""" | |
Starts the help server so that Sphinx search works even in Google Chrome. | |
""" | |
import SimpleHTTPServer | |
import SocketServer | |
from threading import Thread | |
from Queue import Queue | |
import os | |
import logging | |
DOCS, PORT = os.path.join('path','to','docs'), 8000 | |
# basic logging configuration | |
log_fmt = '[%(levelname)s] (%(threadName)-10s) %(message)s' | |
logging.basicConfig(level=logging.DEBUG, format=log_fmt) | |
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): | |
pass | |
def help_server(server_queue): | |
# use SimpleHTTPServer | |
os.chdir(DOCS) | |
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler | |
server = ThreadedTCPServer(("", PORT), Handler) | |
logging.debug("serving at port %d", PORT) | |
server_queue.put(server) | |
server.serve_forever() | |
def start_help_server(): | |
server_queue = Queue() | |
# Start a thread with the server -- that thread will then start one | |
# more thread for each request | |
server_thread = Thread(target=help_server, args=[server_queue]) | |
# Exit the server thread when the main thread terminates | |
server_thread.daemon = True | |
server_thread.start() | |
logging.debug("Server loop running in thread: %s", server_thread.name) | |
return server_queue.get() | |
def shutdown_help_server(server): | |
logging.debug("Shutdown help server") | |
server.shutdown() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment