class BBIOServer(): def __init__(self, port=8000, verbose=False, blocking=True): self._server = BBIOHTTPServer(('',port), BBIORequestHandler) self.blocking = blocking if not(verbose): # A log of every request to the server is written to stderr. # This makes for a lot of printing when using the monitors. # We can avoid this by redirecting stderr to a RequestFilter() # instance: sys.stderr = RequestFilter() def start(self, *pages): """ Takes a list of Page instances, creates html files, and starts the server. """ # Make sure at least one page has been given: if not(pages): print "*Can't start server - no pages provided." return # Make sure pages/ directory exists: if not(os.path.exists(PAGES_DIR)): os.system("mkdir %s" % PAGES_DIR) # Remove old pages if any: if (os.listdir(PAGES_DIR)): os.system("rm %s/*" % PAGES_DIR) # We treat the first page passed in as the home page and create # an index.html page in the base directory that redirects to it: home = pages[0] with open(INDEX, 'w') as index: with open(INDEX_TEMPLATE, 'r') as index_template: index.write(index_template.read() % home.filename) # Generate a list of links for the sidebar: links = '' for page in pages: links += '<li><a href="%s">%s</a></li>\n' % (page.filename, page.title) # Add sidebar to each page and write them to files: #for page in pages: path = "%s/%s" % (PAGES_DIR, page.filename) with open(path, 'w') as f: f.write(str(page) % links) # The server is started as a subprocess using PyBBIO's SafeProcess. # This way it will be non-blocking and stop automatically during # PyBBIO's cleanup routine. self._server_process = SafeProcess(target=self._server.serve_forever) self._server_process.start() if (self.blocking): try: while(True): delay(10000) except KeyboardInterrupt: pass def stop(self): self._server_process.terminate()
class BBIOServer(): def __init__(self, port=8000, verbose=False, blocking=True): self._server = BBIOHTTPServer(('', port), BBIORequestHandler) self.blocking = blocking if not (verbose): # A log of every request to the server is written to stderr. # This makes for a lot of printing when using the monitors. # We can avoid this by redirecting stderr to a RequestFilter() # instance: sys.stderr = RequestFilter() def start(self, *pages): """ Takes a list of Page instances, creates html files, and starts the server. """ # Make sure at least one page has been given: if not (pages): print "*Can't start server - no pages provided." return # Make sure pages/ directory exists: if not (os.path.exists(PAGES_DIR)): os.system("mkdir %s" % PAGES_DIR) # Remove old pages if any: if (os.listdir(PAGES_DIR)): os.system("rm %s/*" % PAGES_DIR) # We treat the first page passed in as the home page and create # an index.html page in the base directory that redirects to it: home = pages[0] with open(INDEX, 'w') as index: with open(INDEX_TEMPLATE, 'r') as index_template: index.write(index_template.read() % home.filename) # Generate a list of links for the sidebar: links = '' for page in pages: links += '<li><a href="%s">%s</a></li>\n' % (page.filename, page.title) # Add sidebar to each page and write them to files: for page in pages: path = "%s/%s" % (PAGES_DIR, page.filename) with open(path, 'w') as f: f.write(str(page) % links) # The server is started as a subprocess using PyBBIO's SafeProcess. # This way it will be non-blocking and stop automatically during # PyBBIO's cleanup routine. self._server_process = SafeProcess(target=self._server.serve_forever) self._server_process.start() if (self.blocking): try: while (True): delay(10000) except KeyboardInterrupt: pass def stop(self): self._server_process.terminate()
def setup(): p = SafeProcess(target=foo) p.start()
def setup(): p = SafeProcess(target=foo) p.start()