title: How can I test if Python http.server.HTTPServer is serving forever?
body: <p>I am writing this little demo code to start an HTTP server, test that it is running successfully and then exit.</p>
  
  <pre><code>import http.server
  import urllib.request
  import threading
  
  # Start HTTP server
  httpd = http.server.HTTPServer(('', 8080), http.server.SimpleHTTPRequestHandler)
  thread = threading.Thread(target=httpd.serve_forever)
  thread.start()
  
  # Test HTTP server and shutdown
  print(urllib.request.urlopen('http://127.0.0.1:8080/').read())
  httpd.shutdown()
  </code></pre>
  
  <p>This seems to be working fine but it might be working fine due to luck. You see, I am invoking <code>httpd.serve_forever</code> in a separate thread, so that my script doesn't block at this call and it can continue with the <code>urllib.request.urlopen</code> call to test the HTTP server.</p>
  
  <p>But I must ensure that I am making the <code>urllib.request.urlopen</code> call only after <code>httpd.serve_forever</code> has been invoked successfully and the server is now serving requests.</p>
  
  <p>Is there any flag or any method in <code>http.server.HTTPServer</code> or <code>socketserver.TCPServer</code> that will help me to ensure that I begin testing it with <code>urllib.request.urlopen</code> only after it is ready to serve requests?</p>