Beispiel #1
0
class StaticFileServer:
    _server_lock = threading.Lock()

    def __init__(self, handler="etag"):
        self._server_lock.acquire()
        handler_class = ETagHandler if handler == "etag" else ContentMD5Handler
        self.bind_port_by_any_means(handler_class)

    def bind_port_by_any_means(self, handler_class):
        import time

        # shutdowning/closing socket/server does not unbind port in time,
        # locking the server also does not bring results, hence
        # this method
        for i in range(10000):
            try:
                self.httpd = HTTPServer(("localhost", 8000), handler_class)
            except Exception:
                time.sleep(0.01)
                continue
            break

    def __enter__(self):
        self.server_thread = threading.Thread(target=self.httpd.serve_forever)
        self.server_thread.daemon = True
        self.server_thread.start()

    def __exit__(self, *args):
        self.httpd.socket.close()
        self.httpd.shutdown()
        self.httpd.server_close()
        self._server_lock.release()
Beispiel #2
0
    def bind_port_by_any_means(self, handler_class):
        import time

        # shutdowning/closing socket/server does not unbind port in time,
        # locking the server also does not bring results, hence
        # this method
        for i in range(10000):
            try:
                self.httpd = HTTPServer(("localhost", 8000), handler_class)
            except Exception:
                time.sleep(0.01)
                continue
            break
Beispiel #3
0
class StaticFileServer:
    __server_lock = threading.Lock()

    def __init__(self, handler="etag"):
        handler_class = ETagHandler if handler == "etag" else ContentMD5Handler

        self.__server_lock.acquire()
        self.httpd = HTTPServer(("localhost", 8000), handler_class)

    def __enter__(self):
        self.server_thread = threading.Thread(target=self.httpd.serve_forever)
        self.server_thread.daemon = True
        self.server_thread.start()

    def __exit__(self, *args):
        self.httpd.shutdown()
        self.httpd.server_close()
        self.__server_lock.release()
Beispiel #4
0
    def __init__(self, handler="etag"):
        handler_class = ETagHandler if handler == "etag" else ContentMD5Handler

        self.__server_lock.acquire()
        self.httpd = HTTPServer(("localhost", 8000), handler_class)
Beispiel #5
0
 def __init__(self, handler="etag"):
     self._lock.acquire()
     handler_class = ETagHandler if handler == "etag" else ContentMD5Handler
     self._httpd = HTTPServer(("localhost", 0), handler_class)
     self._thread = None