0

I run a stoppable http server for testing on localhost in python2.7 (ubuntu 16.04). Additionally, I'd like to have the https server as well, so I tried the following code

try:
  start_new_thread(httpd.serve_forever())
  start_new_thread(httpsd.serve_forever())
except KeyboardInterrupt:
  print("\nClosing the service...")

httpd and httpsd are of type BaseHTTPServer.HTTPServer and listen on 3000 (http) and 3001 (https). For https a self signed certificate is used. Each server runs properly if tested alone, but with the code above, only the first started server thread (in our case httpd) runs in 'multithreaded' mode and delivers data. The server in the second thread simply does not respond to any request...

Can anybody please tell me, what's wrong with the snippet above? Or otherwise show me a solution based on the approach above? Cheers,

x y
  • 911
  • 9
  • 27

1 Answers1

0

Got it; the answer to my problem according to this SO issue is: that instead of

try:
  start_new_thread(httpd.serve_forever())
  start_new_thread(httpsd.serve_forever())
except KeyboardInterrupt:

I have to use true non-blocking function pointer

thread1 = Thread(target = httpd.serve_forever) # Without ()
thread2 = Thread(target = httpsd.serve_forever) # Without ()
try:
  thread1.start()
  thread2.start()
except KeyboardInterrupt:

Ridiculous

x y
  • 911
  • 9
  • 27