Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python SimpleHTTPServer hangs after several requests

I'm writing a simple integration test which consists of the following:

  1. Starting a simple HTTP server which serves up a page to be tested
  2. Starting various different browsers which load this page to confirm that everything works.

I know #2 works and I have no problem with this.

However, #1 only works for the first few browsers and then just hangs, causing subsequent browser tests to fail. I really don't understand why.

In my code here's what I'm doing:

class Test:  
    def setup_class(cls):  
        ready_signal = threading.Event()  
        cls.port = 9000  
        cls.docroot = /tmp  
        cls.http_wrapper = SimpleHTTPWrap(port=cls.port, docroot=cls.docroot, ready_signal)  
        background_thread = threading.Thread(target=http_wrapper.start)  
        background_thread.daemon = True  
        background_thread.start()  
        ready_signal.wait(10) # Wait for thread to be ready (or proceed after timeout)  
    def test(self):  
        ip = get_my_ip_address()  
        self.browser = webdriver.Remote(_some_parameters_go_here)  
        self.browser.get('http://' + ip + ":" + cls.port + cls.docroot + '/test.html')  
        # This hangs after similar tests have already run and succeeded  
    def teardown_class(cls):  
        cls.http_wrapper.stop()    

class SimpleHTTPWrap(object):  
    def __init__(self, port, docroot, ready_signal):  
        self.port = port  
        self.docroot = docroot  
        self.request_handler = SimpleHTTPServer.SimpleHTTPRequestHandler  
        self.server = None  
        self.ready_signal = ready_signal  
    def start(self):  
        os.chdir(self.docroot)  
        try:  
            log.info("Attempting to bind to port: " + str(self.port))  
            self.server = SocketServer.TCPServer(("", self.port), self.request_handler)  
            self.server.allow_reuse_address = True  
            log.info("Success.")  
            self.ready_signal.set() # Signal the main thread that we're ready  
            self.server.serve_forever()  
        except socket.error:  
            log.error("Could not bind to address (port already in use)" + str(self.port))  

I think it's in some sort of deadlock situation because when I tried calling cls.http_wrapper.server.shutdown() it just hung.

However, I have no idea why. There is only ever one thread running here in the background and other tests seem to be fine. But after a few, everything hangs.
From the browser perspective, it tries loading the page for a while until it finally times out due to a connection refused.

Would anyone happen to know what might be causing this or what I can do to further investigate? At this point I'm at a loss and considering just trying something else.

like image 686
Elephantinae Avatar asked Nov 22 '22 10:11

Elephantinae


1 Answers

This happens to me when I get an external connection and the server has a browser with the page opened. When I close the page the external clients can connect flawlessly.

I have a pretty simple HTTP server but I handle the serving using with

with http.server.HTTPServer(("0.0.0.0", PORT), myHandler) as server:
   print("Running on port", PORT)
   server.serve_forever()

I know the question is old but I hope this could help someone.

like image 143
DocPython Avatar answered Dec 16 '22 04:12

DocPython