【问题标题】:TimeoutError: [WinError 10060] A connection attempt failed : Except block doesn't execute eitherTimeoutError: [WinError 10060] 连接尝试失败:除了块不执行
【发布时间】:2022-11-27 19:29:14
【问题描述】:

在我的程序中,我尝试并排除像这样的块

try:
    if exitCritera(ceLiveprc,peLiveprc,ceEntry,peEntry):
        closeAllOpenAndPendingTrades(cTok,pTok,ceEntry,peEntry)
except(TimeoutError) as e:
    print("Timeout error occured re-trying...:{}".format(datetime.now()))
    time.sleep(1)
    if exitCritera(ceLiveprc,peLiveprc,ceEntry,peEntry):
        closeAllOpenAndPendingTrades(cTok,pTok,ceEntry,peEntry)

exitCriteria 函数检查一些数据,然后调用 closeAllOpenAndPendingTrades() 并且在 closeAllOpenAndPendingTrades() 中有与服务器的联系。

我注意到我在打球。

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 174, in _new_conn
    conn = connection.create_connection(
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 96, in create_connection
    raise err
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 86, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

所以我添加了 except 块。但是 except 块根本不执行。 一旦发生超时错误,我能做些什么来重试?

完整的错误日志

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 174, in _new_conn
    conn = connection.create_connection(
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 96, in create_connection
    raise err
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 86, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 382, in _make_request
    self._validate_conn(conn)
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 1010, in _validate_conn
    conn.connect()
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 358, in connect
    conn = self._new_conn()
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x000002219E8422B0>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 440, in send
    resp = conn.urlopen(
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\retry.py", line 574, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='norenapi.thefirstock.com', port=443): Max retries exceeded with url: /NorenWClientTP/PositionBook (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002219E8422B0>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:/Users/cgs/Documents/Personal/Stocks/pycode/client/py_client/test.py", line 1035, in workerThrd
    try:
  File "C:/Users/cgs/Documents/Personal/Stocks/pycode/client/py_client/test.py", line 540, in exitCritera
    
  File "C:/Users/cgs/Documents/Personal/Stocks/pycode/client/py_client/test.py", line 518, in maxProfitHit
    def maxProfitHit(ceLivePrice, peLivePrice,ceSellPrice,peSellPrice):
  File "C:/Users/cgs/Documents/Personal/Stocks/pycode/client/py_client/test.py", line 482, in tradeMaxedOut
    posBook = getPositionBook()
  File "C:/Users/cgs/Documents/Personal/Stocks/pycode/client/py_client/test.py", line 395, in getPositionBook
    posBook = client.orders.position_book(posBook) # This returns a string.
  File "C:\Users\cgs\Documents\Personal\Stocks\pycode\client\py_client\modules\orders\datasource.py", line 190, in position_book
    response_json = self._run_request(model, endpoints.POSITION_BOOK, key)
  File "C:\Users\cgs\Documents\Personal\Stocks\pycode\client\py_client\utils\datasources\noren\datasource.py", line 20, in _run_request
    return self.post(endpoint, f"jData={request_json}&jKey={key}")
  File "C:\Users\cgs\Documents\Personal\Stocks\pycode\client\py_client\utils\datasources\rest\datasource.py", line 68, in post
    response = requests.post(
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 117, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\cgs\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 519, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='norenapi.thefirstock.com', port=443): Max retries exceeded with url: /NorenWClientTP/P

【问题讨论】:

    标签: python python-requests timeoutexception


    【解决方案1】:

    [WinError 10060] 是 Windows 套接字错误。尝试将其捕获为 OSError,这在非常相似的情况下对我有用。

    将您的代码更改为以下内容:

    try:
        if exitCritera(ceLiveprc,peLiveprc,ceEntry,peEntry):
            closeAllOpenAndPendingTrades(cTok,pTok,ceEntry,peEntry)
    except OSError as e:
        print(e)
        print("Timeout error occured re-trying...:{}".format(datetime.now()))
        time.sleep(1)
        if exitCritera(ceLiveprc,peLiveprc,ceEntry,peEntry):
            closeAllOpenAndPendingTrades(cTok,pTok,ceEntry,peEntry)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 2016-12-07
      • 1970-01-01
      • 2018-12-29
      • 2015-05-26
      • 1970-01-01
      相关资源
      最近更新 更多