Last active
June 22, 2025 17:41
-
-
Save oliver-zehentleitner/74f6c5a461b01b5249c44e335ccf4e88 to your computer and use it in GitHub Desktop.
Connect to Binance Websockets using a SOCKS5 Proxy in Python with UNICORN Binance WebSocket API. - How To: https://medium.lucit.tech/how-to-connect-to-binance-com-websockets-using-python-via-a-socks5-proxy-3c5a3e063f12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from unicorn_binance_websocket_api import BinanceWebSocketApiManager, Socks5ProxyConnectionError | |
| import asyncio | |
| import logging | |
| import os | |
| async def binance_stream(ubwa): | |
| def handle_socket_message(data): | |
| print(f"received data: {data}") | |
| ubwa.create_stream(channels=['kline', 'kline_1m'], | |
| markets=['btcusdt'], | |
| output="UnicornFy", | |
| process_stream_data=handle_socket_message) | |
| while True: | |
| await asyncio.sleep(1) | |
| if __name__ == "__main__": | |
| logging.getLogger("unicorn_binance_websocket_api") | |
| logging.basicConfig(level=logging.DEBUG, | |
| filename=os.path.basename(__file__) + '.log', | |
| format="{asctime} [{levelname:8}] {process} {thread} {module}: {message}", | |
| style="{") | |
| socks5_proxy = "1.2.3.4:1080" | |
| socks5_ssl_verification = True | |
| try: | |
| ubwa = BinanceWebSocketApiManager(exchange='binance.com', | |
| socks5_proxy_server=socks5_proxy, | |
| socks5_proxy_ssl_verification=socks5_ssl_verification) | |
| except Socks5ProxyConnectionError as error_msg: | |
| print(f"Socks5ProxyConnectionError: {error_msg}") | |
| exit(1) | |
| try: | |
| asyncio.run(binance_stream(ubwa)) | |
| except KeyboardInterrupt: | |
| print("\r\nGracefully stopping the websocket manager...") | |
| ubwa.stop_manager() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment