带有 http 代理的 python-binance ThreadedWebsocketManager

问题描述 投票:0回答:2

我尝试使用 ThreadedWebsocketManager 在 Windows PC 上启动流。 这台 PC 在 http 代理后面。 没有错误,但没有响应.... 我做错什么了吗?还是我忘记了什么?

注意:在这台电脑上我没有管理权限...

编辑:我试图在没有代理的电脑上启动相同的流及其工作....

编辑:我尝试使用“客户端”获取我的币安帐户信息,设置代理环境变量及其工作,似乎只有“ThreadedWebsocketManager”不起作用。

代码:

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()
python websocket proxy stream binance
2个回答
1
投票

很可能

requests
library biance 在引擎盖下使用看不到你
os.environ
变化。

尝试使用文档中建议的方法之一,例如设置

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})

    # ...

# ...

0
投票

你可以这样做:

安装

pip install unicorn-binance-websocket-api
.

来源: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()
© www.soinside.com 2019 - 2024. All rights reserved.