多个 uri 的请求(asyncio)
Requests by multiple uris (asyncio)
如何使用acyncio
向exchange websocket发送多个请求?
Exchange websockets documentation
事实上,如果您知道如何发送多个请求,例如,使用 asyncio
而不是 websockets
库的 requests
库,那也很棒.
我想使用 asyncio
库向多个 websockets(示例中有两个)异步发送多个请求。只需一个请求,一切都适合我:
import asyncio
import websockets
from autobahn.asyncio.websocket import WebSocketClientProtocol
from typing import List
async def logging_message(_websocket: WebSocketClientProtocol) -> None:
async for message in _websocket:
data = json.loads(message)['data']
print(data)
async def websockets_connect(_websocket_uris: List) -> None:
async with websockets.connect(_websocket_uris[0]) as websocket: ## !!! HERE I use bad thing: _websocket_uris[0]
await logging_message(websocket)
websocket_uris = [
'wss://stream.binance.com:9443/stream?streams=btcusdt@kline_1m/ethusdt@kline_1m/ethbtc@kline_1m/bnbbtc@kline_1m'
] # The list is only of length 1, everything works
asyncio.get_event_loop().run_until_complete(websockets_connect(websocket_uris))
但是我不能在单个请求中使用这样的代码,因为交换对一个uri的长度有限制(_websocket_uris[0]
),所以我需要用不同的 uri 发送多个请求。类似的东西:
async def websockets_connect(_websocket_uri: str) -> None:
async with websockets.connect(_websocket_uri) as websocket:
await logging_message(websocket)
async def main():
await asyncio.gather(*[
websockets_connect(websocket_uri) for websocket_uri in websocket_uris
])
websocket_uris = [
'wss://stream.binance.com:9443/stream?streams=btcusdt@kline_1m/ethusdt@kline_1m',
'wss://stream.binance.com:9443/stream?streams=/ethbtc@kline_1m/bnbbtc@kline_1m'
] # list of length 2, nothing works
asyncio.get_event_loop().run_until_complete(main())
并且它不起作用(错误的请求):
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 400
这个很搞笑,呵呵:
我在第二个 uri 中有错字
'wss://stream.binance.com:9443/stream?streams=/ethbtc@kline_1m/bnbbtc@kline_1m'
到 'wss://stream.binance.com:9443/stream?streams=ethbtc@kline_1m/bnbbtc@kline_1m'
如何使用acyncio
向exchange websocket发送多个请求?
Exchange websockets documentation
事实上,如果您知道如何发送多个请求,例如,使用 asyncio
而不是 websockets
库的 requests
库,那也很棒.
我想使用 asyncio
库向多个 websockets(示例中有两个)异步发送多个请求。只需一个请求,一切都适合我:
import asyncio
import websockets
from autobahn.asyncio.websocket import WebSocketClientProtocol
from typing import List
async def logging_message(_websocket: WebSocketClientProtocol) -> None:
async for message in _websocket:
data = json.loads(message)['data']
print(data)
async def websockets_connect(_websocket_uris: List) -> None:
async with websockets.connect(_websocket_uris[0]) as websocket: ## !!! HERE I use bad thing: _websocket_uris[0]
await logging_message(websocket)
websocket_uris = [
'wss://stream.binance.com:9443/stream?streams=btcusdt@kline_1m/ethusdt@kline_1m/ethbtc@kline_1m/bnbbtc@kline_1m'
] # The list is only of length 1, everything works
asyncio.get_event_loop().run_until_complete(websockets_connect(websocket_uris))
但是我不能在单个请求中使用这样的代码,因为交换对一个uri的长度有限制(_websocket_uris[0]
),所以我需要用不同的 uri 发送多个请求。类似的东西:
async def websockets_connect(_websocket_uri: str) -> None:
async with websockets.connect(_websocket_uri) as websocket:
await logging_message(websocket)
async def main():
await asyncio.gather(*[
websockets_connect(websocket_uri) for websocket_uri in websocket_uris
])
websocket_uris = [
'wss://stream.binance.com:9443/stream?streams=btcusdt@kline_1m/ethusdt@kline_1m',
'wss://stream.binance.com:9443/stream?streams=/ethbtc@kline_1m/bnbbtc@kline_1m'
] # list of length 2, nothing works
asyncio.get_event_loop().run_until_complete(main())
并且它不起作用(错误的请求):
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 400
这个很搞笑,呵呵:
我在第二个 uri 中有错字
'wss://stream.binance.com:9443/stream?streams=/ethbtc@kline_1m/bnbbtc@kline_1m'
到 'wss://stream.binance.com:9443/stream?streams=ethbtc@kline_1m/bnbbtc@kline_1m'