簡體   English   中英

TimeoutError:[WinError 10060] 連接嘗試失敗:除了塊也不執行

[英]TimeoutError: [WinError 10060] A connection attempt failed : Except block doesn't execute either

在我的程序中,我嘗試並排除像這樣的塊

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 function 檢查一些數據,然后調用 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

[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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM