Python 2.7 Python 2在不同线程中运行HTTP和HTTPS服务器时出现问题
我在python2.7(ubuntu16.04)中运行了一个stoppablehttp服务器,用于在本地主机上进行测试。 此外,我还希望使用https服务器,因此我尝试了以下代码Python 2.7 Python 2在不同线程中运行HTTP和HTTPS服务器时出现问题,python-2.7,Python 2.7,我在python2.7(ubuntu16.04)中运行了一个stoppablehttp服务器,用于在本地主机上进行测试。 此外,我还希望使用https服务器,因此我尝试了以下代码 try: start_new_thread(httpd.serve_forever()) start_new_thread(httpsd.serve_forever()) except KeyboardInterrupt: print("\nClosing the service...") httpd和h
try:
start_new_thread(httpd.serve_forever())
start_new_thread(httpsd.serve_forever())
except KeyboardInterrupt:
print("\nClosing the service...")
httpd和httpsd的类型为BaseHTTPServer.HTTPServer,并在3000(http)和3001(https)上侦听。对于https,使用自签名证书。
如果单独测试,每台服务器都能正常运行,但是使用上面的代码,只有第一个启动的服务器线程(在我们的例子中是httpd)以“多线程”模式运行并传递数据。第二个线程中的服务器根本不对任何请求作出响应
谁能告诉我,上面的片段有什么问题吗?
或者以其他方式向我展示基于上述方法的解决方案?
干杯,明白了;我的问题的答案是:那而不是
try:
start_new_thread(httpd.serve_forever())
start_new_thread(httpsd.serve_forever())
except KeyboardInterrupt:
我必须使用真正的非阻塞函数指针
thread1 = Thread(target = httpd.serve_forever) # Without ()
thread2 = Thread(target = httpsd.serve_forever) # Without ()
try:
thread1.start()
thread2.start()
except KeyboardInterrupt:
可笑的