multithreading - How can I test if Python http.server.HTTPServer is serving forever? -


i writing little demo code start http server, test running , exit.

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 , shutdown print(urllib.request.urlopen('http://127.0.0.1:8080/').read()) httpd.shutdown() 

this seems working fine might working fine due luck. see, invoking httpd.serve_forever in separate thread, script doesn't block @ call , can continue urllib.request.urlopen call test http server.

but must ensure making urllib.request.urlopen call after httpd.serve_forever has been invoked , server serving requests.

is there flag or method in http.server.httpserver or socketserver.tcpserver me ensure begin testing urllib.request.urlopen after ready serve requests?

if goal not empty / invalid answer in urlopen, answer won't. open socket line:

http.server.httpserver(...) 

so after (with still 1 thread running) already have open socket listening incoming connections (although nothing dispatching requests yet). after may have race of 2 threads. consider both possibilities:

  1. the server thread runs serve_forever first. well, it's case you're aiming at.
  2. the main thread connects first. connection accepted , wait dispatch handler. server thread runs serve_forever, attaches dispatcher , request still gets processed.

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -