Bad handshake problem

I’d like to know what this problem is. I’m facing it almost daily and it makes my binance strategy unusable.
I don’t use any wrapper or libraries. I’m on MacOS, python 3.7.1 (problem persists on other versions too).
I should be far away from rate limit. The time when this happens there was only my listenkey update running. My network connection at that time is monitored and stable.

the error:

lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 06:06:42
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 07:05:43
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 08:04:44
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 09:03:45
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 10:02:46
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 11:01:47
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 12:00:48
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 12:59:49
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 13:58:50
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 14:57:51
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 15:56:51
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 16:55:52
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 17:54:53
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 18:53:54
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 19:52:55
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 20:51:55
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 21:50:56
lk_keep_alive: <Response [200]> exfd94qZZJeEcDLmcHodGFYxv6plqCjjlDbLPLICkASFEhigsrpw45GBE4tOyUTt 2021-08-28 22:49:57
Traceback (most recent call last):
  File "/Users/username/anaconda3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py", line 453, in wrap_socket
    cnx.do_handshake()
  File "/Users/username/anaconda3/lib/python3.7/site-packages/OpenSSL/SSL.py", line 1907, in do_handshake
    self._raise_ssl_error(self._ssl, result)
  File "/Users/username/anaconda3/lib/python3.7/site-packages/OpenSSL/SSL.py", line 1631, in _raise_ssl_error
    raise SysCallError(errno, errorcode.get(errno))
OpenSSL.SSL.SysCallError: (60, 'ETIMEDOUT')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/username/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/Users/username/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py", line 343, in _make_request
    self._validate_conn(conn)
  File "/Users/username/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py", line 839, in _validate_conn
    conn.connect()
  File "/Users/username/anaconda3/lib/python3.7/site-packages/urllib3/connection.py", line 344, in connect
    ssl_context=context)
  File "/Users/username/anaconda3/lib/python3.7/site-packages/urllib3/util/ssl_.py", line 344, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/Users/username/anaconda3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py", line 459, in wrap_socket
    raise ssl.SSLError('bad handshake: %r' % e)
ssl.SSLError: ("bad handshake: SysCallError(60, 'ETIMEDOUT')",)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/username/anaconda3/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/Users/username/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/Users/username/anaconda3/lib/python3.7/site-packages/urllib3/util/retry.py", line 398, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='fapi.binance.com', port=443): Max retries exceeded with url: /fapi/v1/listenKey (Caused by SSLError(SSLError("bad handshake: SysCallError(60, 'ETIMEDOUT')")))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/username/Workfiles/python/binance/vs_binance_algo/external_listenkey_update.py", line 48, in <module>
    start = keepAlive()                
  File "/Users/username/Workfiles/python/binance/vs_binance_algo/external_listenkey_update.py", line 35, in keepAlive
    update_LK = requests.request('POST', url_LK, headers=headers, data=payload)
  File "/Users/username/anaconda3/lib/python3.7/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Users/username/anaconda3/lib/python3.7/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/username/anaconda3/lib/python3.7/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/Users/username/anaconda3/lib/python3.7/site-packages/requests/adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='fapi.binance.com', port=443): Max retries exceeded with url: /fapi/v1/listenKey (Caused by SSLError(SSLError("bad handshake: SysCallError(60, 'ETIMEDOUT')")))
(base) MBP2012%

Can you try using the requests library instead of urllib3 and see if the problem persist?

Chai
Can you try using the requests library instead of urllib3 and see if the problem persist?

I use the requests library, but requests uses internally urllib3

Hi. Can you remove the port number and try again?

I didn’t state any port number. As far as I know, 443 is the default port if you don’t add one. Please let me know if you know how to remove it

update_LK = requests.request('POST', url_LK, headers=headers, data=payload)

Try to change from POST to PUT (https://binance-docs.github.io/apidocs/futures/en/#keepalive-user-data-stream-user_stream) to keepalive a listenKey.

Ok I’ll try it. thank you for the tip

just a side note, please also ensure that the base url is “https://fapi.binance.com” and not just “fapi.binance.com