python HTTP服务器不接受连接

python HTTP服务器不接受连接,python,Python,我想弄明白为什么curlhttp://localhost:2113/healthz失败,curl:(56)Recv失败:对等方重置连接 main.py启动HealthServer: from healthServer import HealthServer if __name__ == '__main__': HealthServer().start() healthServer.py将healthServer定义为使用从BaseHTTPRequestHandler派生的处理程序为HTT

我想弄明白为什么
curlhttp://localhost:2113/healthz
失败,curl:(56)Recv失败:对等方重置连接

main.py启动HealthServer:

from healthServer import HealthServer

if __name__ == '__main__':
  HealthServer().start()
healthServer.py将healthServer定义为使用从BaseHTTPRequestHandler派生的处理程序为HTTP提供服务的线程:

from configuration import config
from http.server import HTTPStatus, HTTPServer, BaseHTTPRequestHandler
import logging
from threading import Thread

class HealthHTTPRequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        '''Respond to a GET request.'''
        if self.path == '/healthz':
            logging.info('GET /Healthz')
            self.send_response(HTTPStatus.OK)
            self.end_headers()
            self.wfile.write(b'ok')
        else:
            logging.error('health endpoint received a GET for path {}'.format(self.path))
            self.send_response(HTTPStatus.NOT_FOUND)
            self.end_headers()

class HealthServer(Thread):
    def __init__(self):
        super().__init__()

    def run(self):
        port=config['server']['ports']['healthz'].get()
        logging.info('Starting HTTP server on port {} to serve health on /healthz'.format(port))
        httpd = HTTPServer(('localhost', port), HealthHTTPRequestHandler)
        httpd.serve_forever()
        logging.warning('health server exited')
启动应用程序日志
在端口2113上启动HTTP服务器以在/healthz上提供运行状况服务

如果我去掉涉及
config
的几行代码,只使用硬代码
port=2113
,对我来说效果很好<代码>http://localhost:2113/healthz在浏览器中给出
ok
,并在PyCharm控制台中给出
127.0.0.1---[23/Oct/2020 13:47:47]“GET/healthz HTTP/1.1”200-
我应该注意的是,我把两个代码片段放在同一个文件中,这样我就可以使用我的标准暂存区来测试任意代码。我想你的主线程在启动http服务器后刚刚退出,所以整个应用程序都死掉了。您可以.join()您的服务器线程等待它,或者根本不使用服务器线程(只使用主线程)。您不需要做任何事情就可以让主线程不停止整个程序。我没有改变任何事情,也没有放弃。只要不将线程标记为守护进程线程,系统将等待线程退出以停止程序。主线程没有什么特别之处。有趣的是,我还将所有内容复制到一个文件中,并对端口进行了硬编码。启动应用程序,指向它,返回“ok”。我想我的问题一定是来自docker。我通常在docker运行命令中使用-p 2113:2113在docker和port forward内部运行此命令,然后它就不工作了。调查…事实上,docker问题,这个应用程序没有问题。如果我将docker exec放到容器上并卷曲,它就会工作。不确定发生了什么,但这不再是python的问题了。非常感谢。