简体   繁体   中英

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

In my program i have a try and except block like so

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)

the exitCriteria function checks for some data and then calls closeAllOpenAndPendingTrades() and in closeAllOpenAndPendingTrades() there is a contact to the server.

I noticed that i was hitting.

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

So i added the except block. But the except block doesn't execute at all. Is there anything i can do to retry once a timeout error is hit?

Complete error log

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] is a Windows Socket Error. Try catching it as an OSError, that worked for me in a very similar case.

Change your code to the following:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM