Is there an API to close the connection

Hello there,

I am running a bot on the Futures market and I would like to be able to close my connection whenever I want as it seems Binance is automatically closing connections from time to time. Is there an API to close a connection ? If there is no such an API, are there other alternatives ?

Note: I am using this module binance.um_futures, but I am open to using something else providing it fixes my issue. So, I am opening a connection using this method

myClient = UMFutures(API_KEY, API_SECRET)

and I would like to close it later in the code when needed.

Thanks,
Moussa

Hey,
Is your connection automatically closing due to an error? If so, could you share the error message you’re receiving?

If you’re using WebSocket endpoints, keep in mind that the connection will close if you don’t send a pong frame after receiving a ping frame. Additionally, since the connection is only valid for 24 hours, you will need to reconnect. For more information: Websocket API General Info | Binance Open Platform

To disconnect from a WebSocket API endpoint, you can use the session.logout endpoint.

To unsubscribe from a WebSocket stream, you can use the UNSUBSCRIBE method. Live Subscribing Unsubscribing To Streams | Binance Open Platform

Thanks Albin for your answer. Yes, my connection is getting closed from time to time. The error thrown is “Remote end closed connection without response”. Below one of the latest:

==============================
Traceback (most recent call last):
File “/usr/lib/python3/dist-packages/urllib3/connectionpool.py”, line 791, in urlopen
response = self._make_request(
^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/urllib3/connectionpool.py”, line 537, in _make_request
response = conn.getresponse()
^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/urllib3/connection.py”, line 461, in getresponse
httplib_response = super().getresponse()
^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3.12/http/client.py”, line 1428, in getresponse
response.begin()
File “/usr/lib/python3.12/http/client.py”, line 331, in begin
version, status, reason = self._read_status()
^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3.12/http/client.py”, line 300, in _read_status
raise RemoteDisconnected(“Remote end closed connection without”
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/usr/lib/python3/dist-packages/requests/adapters.py”, line 486, in send
resp = conn.urlopen(
^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/urllib3/connectionpool.py”, line 845, in urlopen
retries = retries.increment(
^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/urllib3/util/retry.py”, line 472, in increment
raise reraise(type(error), error, _stacktrace)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/urllib3/util/util.py”, line 38, in reraise
raise value.with_traceback(tb)
File “/usr/lib/python3/dist-packages/urllib3/connectionpool.py”, line 791, in urlopen
response = self._make_request(
^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/urllib3/connectionpool.py”, line 537, in _make_request
response = conn.getresponse()
^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/urllib3/connection.py”, line 461, in getresponse
httplib_response = super().getresponse()
^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3.12/http/client.py”, line 1428, in getresponse
response.begin()
File “/usr/lib/python3.12/http/client.py”, line 331, in begin
version, status, reason = self._read_status()
^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3.12/http/client.py”, line 300, in _read_status
raise RemoteDisconnected(“Remote end closed connection without”
urllib3.exceptions.ProtocolError: (‘Connection aborted.’, RemoteDisconnected(‘Remote end closed connection without response’))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/var/www/html/Tradingview/usdtScanner.py”, line 613, in
balance = get_balance_usdt(myClient)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/var/www/html/Tradingview/usdtScanner.py”, line 72, in get_balance_usdt
response = clientObj.balance()
^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/dist-packages/binance/um_futures/account.py”, line 548, in balance
return self.sign_request(“GET”, url_path, {**kwargs})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/dist-packages/binance/api.py”, line 83, in sign_request
return self.send_request(http_method, url_path, payload, special)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/dist-packages/binance/api.py”, line 116, in send_request
response = self._dispatch_request(http_method)(**params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/requests/sessions.py”, line 602, in get
return self.request(“GET”, url, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/requests/sessions.py”, line 589, in request
resp = self.send(prep, **send_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/requests/sessions.py”, line 703, in send
r = adapter.send(request, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3/dist-packages/requests/adapters.py”, line 501, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: (‘Connection aborted.’, RemoteDisconnected(‘Remote end closed connection without response’))

========================
Thanks for your time.

This may be due to a network issue. Check your connection and try reinitializing it.

Thanks for your answer. I don’t think it’s a network issue. I am using a well known VPS company, and not my private computer at home.