python3 ConnectionRefusedError: [Errno 61] 执行线程时连接被拒绝 http.server

时间:2021-04-01 11:41:00

标签: python python-3.x multithreading sockets http.server

我有一个使用 python 3.8 http.server 创建的简单 HTTP 服务器。

2 个端点 1: /restart 在后台执行一个线程 2:/check_status

当我请求第一个端点时,它正在执行良好的第二个请求,直到线程执行完成,才会收到套接字连接被拒绝的错误。 如果有更好的方法,请告诉我。

def restart_solr_service():
    tr = SolrRestartThread()


class SolrRestartThread(object):
    def __init__(self, interval=1):
        self.interval = interval
thread = threading.Thread(target=self.run, args=())
        thread.daemon = True
        thread.start()

    def run(self):

        logger.info(execute_cmd(solr_restart_cmd,status_workspace,logger))
        logger.info("Restart solr completed")
        wait_for_solr_service()

    class MyServer(BaseHTTPRequestHandler):
    def do_GET(self):
        #self.send_response(200)

        response_code= 200
        request_key = self.path.replace('/', '')
        if request_key in get_op_dict:
            print(request_key)
            body_content,response_code = get_op_dict[request_key]()
            print(response_code)
        else:
            
            body_content = "invalid request.\n<br>Hostname:" + socket.gethostname()

        self.send_response(response_code)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write(bytes("<html><body>", "utf-8"))
        self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
        self.wfile.write(bytes("<p>" + body_content + " </p> ", "utf-8"))
        self.wfile.write(bytes("<p>Response code:" + str(response_code) + " </p> ", "utf-8"))
        self.wfile.write(bytes("</body></html>", "utf-8"))
        


0 个答案:

没有答案
相关问题