Connect binance testnet futures user stream using python-binance

331 Views Asked by At

I'm going to connect binance testnet futures user stream data using python-binance library. I tried following code:

async def update_crypto_user_websocket():
    async_client = get_async_client(True)
    socket_manager = get_binance_socket_manager(async_client)
    user_stream = socket_manager.futures_user_socket()
    # then start receiving messages
    async with user_stream as ts:
        while True:
            res = await ts.recv()
            print(res)


async def main():
    await asyncio.gather(update_crypto_user_websocket())

if __name__ == "__main__":
    asyncio.run(main())

get_async_client and get_binance_socket_manager are defined as below:

async_client = None
binance_socket_manager = None

def get_async_client(is_testnet: bool, api_key: str = 'API_KEY', secret_key: str = 'SECRET_KEY'):
    try:
        global async_client
        if async_client is None:
            async_client = AsyncClient(api_key, secret_key, testnet=is_testnet)
        return async_client
    except Exception as err:
        print(err)
        raise HTTPException(status_code=404, detail="Binance not reachable")


def get_binance_socket_manager(async_client_arg: AsyncClient):
    try:
        global binance_socket_manager
        if binance_socket_manager is None:
            binance_socket_manager = BinanceSocketManager(async_client_arg)
        return binance_socket_manager
    except Exception as err:
        print(err)
        raise HTTPException(status_code=404, detail="Binance not reachable")

This socket waits until any change happens in account but when I run this code, nothing happens even if I submit or cancel order in binance testnet futures.

What are the problems with this code and how can I get user socket data?

0

There are 0 best solutions below