-1

I try to use ThreadedWebsocketManager to start a stream on a Windows PC. This PC is behind a http proxy. No error, but no response.... did I do something wrong? or did I forget something?

Note: on this pc i don't have Administrative rights...

EDIT: i tried to start the same stream in a pc WITHOUT proxy and its work....

EDIT: i tried to use "client" to get my binance account info, setting the PROXY enviroment variable and its work, it seems only "ThreadedWebsocketManager" dosen't work.

code:

from binance import ThreadedWebsocketManager
import os

proxy = "http://<username>:<password>@<proxyurl>:<port>"
os.environ['http_proxy'] = proxy
os.environ['HTTP_PROXY'] = proxy
os.environ['https_proxy'] = proxy
os.environ['HTTPS_PROXY'] = proxy

api_key = '<my api key>'
api_secret = '<my secrect key>'

def main():
    symbol = 'BTCUSDT'

    twm = ThreadedWebsocketManager(api_key=api_key, api_secret=api_secret)
    # start is required to initialise its internal loop
    twm.start()

    def handle_socket_message(msg):
        print(f"message type: {msg['e']}")
        print(msg)

    twm.start_kline_socket(callback=handle_socket_message, symbol=symbol)
    twm.join()


if __name__ == "__main__":
   main()
  • most likely biance ThreadedWebsocketManager does not read it's proxy information from the variables you set. – criztovyl Sep 09 '22 at 15:27

2 Answers2

1

most likely the requests libarary biance uses under-the-hood does not see you os.environ changes.

Try using one of the suggested methods from the docs, for example settings request_params:

# ...

proxy =  "..."

proxies = {
    'http': proxy,
    'https': proxy,
}

# ...

def main():

    # ...

    # note the added request_params
    twm = ThreadedWebsocketManager(api_key=api_key, api_secret=api_secret, request_params={'proxies': proxies})

    # ...

# ...
criztovyl
  • 761
  • 5
  • 21
0

You could do this like this:

Install pip install unicorn-binance-websocket-api.

Source: https://pypi.org/project/unicorn-binance-websocket-api

from unicorn_binance_websocket_api.manager import BinanceWebSocketApiManager
from unicorn_binance_websocket_api.exceptions import Socks5ProxyConnectionError
import asyncio
import logging
import os


socks5_proxy = "127.0.0.1:9050"
socks5_ssl_verification = True


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="{")

    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_with_all_streams()
Oliver
  • 105
  • 4