操纵Websocket值

发布于 2025-01-27 04:42:48 字数 1293 浏览 3 评论 0原文

我正在尝试第一次与Python处理Websocket。

我创建了两个客户,我想对这两个结果进行一些计算。 (哪个是实时加密价格)

是否可以添加或乘以数字的这些结果?

import websockets
import asyncio
import json
from binance import AsyncClient, BinanceSocketManager

async def upbit_ws_client():
    uri = "wss://api.upbit.com/websocket/v1"
    async with websockets.connect(uri) as websocket:
        subscribe_fmt = [
            {"ticket": "test"},
            {
                "type": "ticker",
                "codes": ["KRW-BTC"],
                "isOnlyRealtime": True
            },
            {"format": "SIMPLE"}
        ]
        subscribe_data = json.dumps(subscribe_fmt)
        await websocket.send(subscribe_data)

        while True:
            data = await websocket.recv()
            data = json.loads(data)
            print(data['cd'], data['hp'])


async def binance_ws_client():
    client = await AsyncClient.create()
    bm = BinanceSocketManager(client)
    ts = bm.symbol_book_ticker_socket("BTCUSDT")

    async with ts as tscm:
        while True:
            res = await tscm.recv()
            print(res['b'], res['a'])


if __name__ == '__main__':
    my_loop = asyncio.get_event_loop()
    my_loop.run_until_complete(asyncio.gather(*[upbit_ws_client(), binance_ws_client()]))
    my_loop.close()

I'm trying to handling websockets with python for the first time.

I created two clients and I want to do some calculate with these two results.
(which is realtime crypto price)

Is it possible to add or multiply numbers of theses result?

import websockets
import asyncio
import json
from binance import AsyncClient, BinanceSocketManager

async def upbit_ws_client():
    uri = "wss://api.upbit.com/websocket/v1"
    async with websockets.connect(uri) as websocket:
        subscribe_fmt = [
            {"ticket": "test"},
            {
                "type": "ticker",
                "codes": ["KRW-BTC"],
                "isOnlyRealtime": True
            },
            {"format": "SIMPLE"}
        ]
        subscribe_data = json.dumps(subscribe_fmt)
        await websocket.send(subscribe_data)

        while True:
            data = await websocket.recv()
            data = json.loads(data)
            print(data['cd'], data['hp'])


async def binance_ws_client():
    client = await AsyncClient.create()
    bm = BinanceSocketManager(client)
    ts = bm.symbol_book_ticker_socket("BTCUSDT")

    async with ts as tscm:
        while True:
            res = await tscm.recv()
            print(res['b'], res['a'])


if __name__ == '__main__':
    my_loop = asyncio.get_event_loop()
    my_loop.run_until_complete(asyncio.gather(*[upbit_ws_client(), binance_ws_client()]))
    my_loop.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杯敬自由 2025-02-03 04:42:48

结果是字符串,因此您需要首先将它们转换为浮点,以使您可以计算:

a = float(res['a'])
b = float(res['b'])

然后您可以提取ab

result = a - b
print(result)

您可以将其添加到代码下:

import websockets
import asyncio
import json
from binance import AsyncClient, BinanceSocketManager

async def upbit_ws_client():
    uri = "wss://api.upbit.com/websocket/v1"
    async with websockets.connect(uri) as websocket:
        subscribe_fmt = [
            {"ticket": "test"},
            {
                "type": "ticker",
                "codes": ["KRW-BTC"],
                "isOnlyRealtime": True
            },
            {"format": "SIMPLE"}
        ]
        subscribe_data = json.dumps(subscribe_fmt)
        await websocket.send(subscribe_data)

        while True:
            data = await websocket.recv()
            data = json.loads(data)
            print(data['cd'], data['hp'])


async def binance_ws_client():
    client = await AsyncClient.create()
    bm = BinanceSocketManager(client)
    ts = bm.symbol_book_ticker_socket("BTCUSDT")

    async with ts as tscm:
        while True:
            res = await tscm.recv()
            print(res['b'], res['a'])
            
            a = float(res['a'])
            b = float(res['b'])
            result = a - b
            print(result)            


if __name__ == '__main__':
    my_loop = asyncio.get_event_loop()
    my_loop.run_until_complete(asyncio.gather(*[upbit_ws_client(), binance_ws_client()]))
    my_loop.close()

The results are strings so you need to convert them to floats first in order to have a data type which you can calculate:

a = float(res['a'])
b = float(res['b'])

Then you can substract a and b:

result = a - b
print(result)

which you can add to your code like below:

import websockets
import asyncio
import json
from binance import AsyncClient, BinanceSocketManager

async def upbit_ws_client():
    uri = "wss://api.upbit.com/websocket/v1"
    async with websockets.connect(uri) as websocket:
        subscribe_fmt = [
            {"ticket": "test"},
            {
                "type": "ticker",
                "codes": ["KRW-BTC"],
                "isOnlyRealtime": True
            },
            {"format": "SIMPLE"}
        ]
        subscribe_data = json.dumps(subscribe_fmt)
        await websocket.send(subscribe_data)

        while True:
            data = await websocket.recv()
            data = json.loads(data)
            print(data['cd'], data['hp'])


async def binance_ws_client():
    client = await AsyncClient.create()
    bm = BinanceSocketManager(client)
    ts = bm.symbol_book_ticker_socket("BTCUSDT")

    async with ts as tscm:
        while True:
            res = await tscm.recv()
            print(res['b'], res['a'])
            
            a = float(res['a'])
            b = float(res['b'])
            result = a - b
            print(result)            


if __name__ == '__main__':
    my_loop = asyncio.get_event_loop()
    my_loop.run_until_complete(asyncio.gather(*[upbit_ws_client(), binance_ws_client()]))
    my_loop.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文