代码之家  ›  专栏  ›  技术社区  ›  mike rodent

为什么我在这里得到WinError 10061(连接被拒绝)?

  •  0
  • mike rodent  · 技术社区  · 4 年前

    我只是做了一些代码来安装,然后启动w10elasticsearch服务,如果它还没有启动和运行的话。当我在没有安装ES服务的情况下运行此操作时,我在 requests.get( ...) (这会扼杀剧本)。

    返回OK,没有抛出内部ES错误。这表明脚本确实安装并启动了服务。。。但由于某些原因,在之后立即发送URL请求(相同的运行/进程)会导致问题。

    顺便说一句,如果我只是停止服务,所以它仍然安装,但停止,然后运行脚本,它再次犯规(即在简单地启动服务)和错误发生。

    更重要的是,我很确定昨天我开始试验的时候没有发生这种情况。出于这个原因,我在我的回购中回到了最简单的实现,希望有人能解释出发生了什么以及如何(是否)解决它。也就是说,我希望能够在同一个Python运行(进程)中安装、启动服务,然后开始使用它。

    import os, sys, io, requests, psutil, elasticsearch
    
    def install_and_start_es_service():
        def get_service(name):
            service = None
            try:
                service = psutil.win_service_get(name)
                service = service.as_dict()
            except Exception as ex:
                print( f'service {name} not yet installed' )
            return service
        try:
            destination_extract_dir = 'D:/apps/ElasticSearch'
            es_bin_dir = destination_extract_dir + '/elasticsearch-7.10.2/bin'
            service = get_service('elasticsearch-service-x64')
            if service and service['status'] == 'running':
                # best case scenario
                return True
            # at this point we will need to try to either start or install the service.
            # to do either of those we need to ensure that the ES bin directory is in place
            if not os.path.isdir( es_bin_dir ):    
                print( 'ES bin dir not found' )
                return False 
            os.chdir( es_bin_dir )
            if not service:
                # can we install the service?
                print( f'before service.bat install...' )
                os.chdir( es_bin_dir )
                os.system( 'elasticsearch-service.bat install' )
                print( f'after service.bat install...' )
            service = get_service('elasticsearch-service-x64')
            if not service:
                print( 'something went wrong trying to install the ES service')
                return False    
            # can we start the service?
            print( f'before service.bat start...' )
            os.chdir( es_bin_dir )
            os.system( 'elasticsearch-service.bat start' )
            print( f'after service.bat start...' )
            service = get_service('elasticsearch-service-x64')
            if service == None or service['status'] != 'running':
                print( 'something went wrong trying to start the ES service')
                return False
        except Exception as e:
            logger.exception( 'unexpected exception thrown while trying to install and start ES service' )
            return False    
        return True
        
    es_started = install_and_start_es_service()
    print( f'ES started? {es_started}')
    if not es_started:
        sys.exit()
    r = requests.get('http://localhost:9200')
    print( f'type r {type(r)}') # requests.models.Response
    

    当出现问题时,这是stacktrace的典型结尾:

      File "D:\more software projects\python_venv\env\lib\site-packages\requests\adapters.py", line 516, in send
        raise ConnectionError(e, request=request)
    requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=9200): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000020842FAAD30>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
    

    请注意,这是一个内部ES错误。。。i、 e.跟踪不是源于我的代码行。然而,我可以计算出 requests.get( ...

    编辑

    leandrojmp很有帮助地建议,可能需要一个非零等待期。我应该说我试过这个 time.sleep( 5 ) . 我想这应该足够了。。。但我将尝试更多的实验沿着这些路线。。。

    0 回复  |  直到 4 年前
        1
  •  0
  •   leandrojmp    4 年前

    您得到的错误是来自请求库的错误,因为您的elasticsearch实例尚未就绪。

    无法建立连接,因为目标计算机主动拒绝了它

    这意味着您的elasticsearch正在运行,端口已打开,但尚未准备好响应请求,因为它仍在启动。

    从您在windows机器上启动Elasticsearch服务到Elasticsearch服务能够真正响应请求,可能需要几秒钟,有时甚至几分钟。