Skip to content

Instantly share code, notes, and snippets.

@MtkN1
Created February 5, 2024 15:15
Show Gist options
  • Save MtkN1/430952f642440778d597b376e79957b8 to your computer and use it in GitHub Desktop.
Save MtkN1/430952f642440778d597b376e79957b8 to your computer and use it in GitHub Desktop.
Rate limit test for GMO Coin WebSocket
import asyncio
import json
import logging
from contextlib import AsyncExitStack
import websockets
logger = logging.getLogger("websockets.client")
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(
logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
)
logger.addHandler(ch)
async def main():
public_uri = "wss://api.coin.z.com/ws/public/v1"
public_message = {"command": "subscribe", "channel": "ticker", "symbol": "BTC_JPY"}
public_message = json.dumps(public_message)
private_uri = "wss://api.coin.z.com/ws/private/v1/xxxxxxxxxxxxxxxxxxxx"
private_message = {"command": "subscribe", "channel": "positionSummaryEvents"}
private_message = json.dumps(private_message)
async with AsyncExitStack() as stack:
async with asyncio.TaskGroup() as tg:
t_public_ws = tg.create_task(
stack.enter_async_context(websockets.connect(public_uri))
)
t_private_ws = tg.create_task(
stack.enter_async_context(websockets.connect(private_uri))
)
public_ws, private_ws = t_public_ws.result(), t_private_ws.result()
async with asyncio.TaskGroup() as tg:
tg.create_task(public_ws.send(public_message))
tg.create_task(private_ws.send(private_message))
await asyncio.Future()
try:
asyncio.run(main())
except KeyboardInterrupt:
pass
@MtkN1
Copy link
Author

MtkN1 commented Feb 5, 2024

Subscriptions have successfully... 🤔

2024-02-06 00:11:54,985 - websockets.client - DEBUG - = connection is CONNECTING
2024-02-06 00:12:13,300 - websockets.client - DEBUG - > GET /ws/private/v1/njGjIYNL5oLTBBNx86rOBPkLLQaQ0KlrdvVqgvCsAOBcWycBEgjGX5ay99lr HTTP/1.1
2024-02-06 00:12:13,300 - websockets.client - DEBUG - > Host: api.coin.z.com
2024-02-06 00:12:13,300 - websockets.client - DEBUG - > Upgrade: websocket
2024-02-06 00:12:13,300 - websockets.client - DEBUG - > Connection: Upgrade
2024-02-06 00:12:13,300 - websockets.client - DEBUG - > Sec-WebSocket-Key: OWRAVwsOA9HucOx56k/uOQ==
2024-02-06 00:12:13,300 - websockets.client - DEBUG - > Sec-WebSocket-Version: 13
2024-02-06 00:12:13,301 - websockets.client - DEBUG - > Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
2024-02-06 00:12:13,301 - websockets.client - DEBUG - > User-Agent: Python/3.12 websockets/12.0
2024-02-06 00:12:13,336 - websockets.client - DEBUG - < HTTP/1.1 101 Switching Protocols
2024-02-06 00:12:13,336 - websockets.client - DEBUG - < Date: Mon, 05 Feb 2024 15:12:13 GMT
2024-02-06 00:12:13,336 - websockets.client - DEBUG - < Connection: upgrade
2024-02-06 00:12:13,336 - websockets.client - DEBUG - < Upgrade: websocket
2024-02-06 00:12:13,337 - websockets.client - DEBUG - < Sec-WebSocket-Accept: smOGAWJghNXkX7K576030VbzhBo=
2024-02-06 00:12:13,337 - websockets.client - DEBUG - < Sec-WebSocket-Extensions: permessage-deflate; server_no_context_takeover; client_no_context_takeover
2024-02-06 00:12:13,337 - websockets.client - DEBUG - < X-Cache: Miss from cloudfront
2024-02-06 00:12:13,337 - websockets.client - DEBUG - < Via: 1.1 fd7e3a18100f88ff2f1e21cc8f9721be.cloudfront.net (CloudFront)
2024-02-06 00:12:13,337 - websockets.client - DEBUG - < X-Amz-Cf-Pop: NRT51-P2
2024-02-06 00:12:13,337 - websockets.client - DEBUG - < X-Amz-Cf-Id: svxLNPX7malCLhzxlkTiRPOzqruiCmVjWybyY1LeXEjzlXmRmUym4w==
2024-02-06 00:12:13,337 - websockets.client - DEBUG - = connection is OPEN
2024-02-06 00:12:13,381 - websockets.client - DEBUG - = connection is CONNECTING
2024-02-06 00:12:13,395 - websockets.client - DEBUG - > GET /ws/public/v1 HTTP/1.1
2024-02-06 00:12:13,395 - websockets.client - DEBUG - > Host: api.coin.z.com
2024-02-06 00:12:13,395 - websockets.client - DEBUG - > Upgrade: websocket
2024-02-06 00:12:13,395 - websockets.client - DEBUG - > Connection: Upgrade
2024-02-06 00:12:13,395 - websockets.client - DEBUG - > Sec-WebSocket-Key: 3QGV1CYiFmLek8Qqfs75dg==
2024-02-06 00:12:13,395 - websockets.client - DEBUG - > Sec-WebSocket-Version: 13
2024-02-06 00:12:13,395 - websockets.client - DEBUG - > Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
2024-02-06 00:12:13,395 - websockets.client - DEBUG - > User-Agent: Python/3.12 websockets/12.0
2024-02-06 00:12:13,431 - websockets.client - DEBUG - < HTTP/1.1 101 Switching Protocols
2024-02-06 00:12:13,431 - websockets.client - DEBUG - < Date: Mon, 05 Feb 2024 15:12:13 GMT
2024-02-06 00:12:13,431 - websockets.client - DEBUG - < Connection: upgrade
2024-02-06 00:12:13,431 - websockets.client - DEBUG - < Upgrade: websocket
2024-02-06 00:12:13,431 - websockets.client - DEBUG - < Sec-WebSocket-Accept: 7t6s0Sh3+u2ZYSGOzqJMkoVwU2c=
2024-02-06 00:12:13,431 - websockets.client - DEBUG - < Sec-WebSocket-Extensions: permessage-deflate; server_no_context_takeover; client_no_context_takeover
2024-02-06 00:12:13,432 - websockets.client - DEBUG - < X-Cache: Miss from cloudfront
2024-02-06 00:12:13,432 - websockets.client - DEBUG - < Via: 1.1 10669864d9506e9be77be128d971849a.cloudfront.net (CloudFront)
2024-02-06 00:12:13,432 - websockets.client - DEBUG - < X-Amz-Cf-Pop: NRT51-P2
2024-02-06 00:12:13,432 - websockets.client - DEBUG - < X-Amz-Cf-Id: aGRmyfeSuPUZON1zwYl-l_rMCY7bFJMx0CoKI67nDPPzHF4gWSIuCg==
2024-02-06 00:12:13,432 - websockets.client - DEBUG - = connection is OPEN
2024-02-06 00:12:13,432 - websockets.client - DEBUG - > TEXT '{"command": "subscribe", "channel": "ticker", "symbol": "BTC_JPY"}' [66 bytes]
2024-02-06 00:12:13,432 - websockets.client - DEBUG - > TEXT '{"command": "subscribe", "channel": "positionSummaryEvents"}' [60 bytes]
2024-02-06 00:12:13,454 - websockets.client - DEBUG - < TEXT '{"averagePositionRate":"81.535","channel":"posi...4-02-05T15:12:13.481Z"}' [229 bytes]
2024-02-06 00:12:14,588 - websockets.client - DEBUG - < TEXT '{"channel":"ticker","ask":"6387190","bid":"6386...2Z","volume":"1191.06"}' [179 bytes]
2024-02-06 00:12:16,918 - websockets.client - DEBUG - < TEXT '{"channel":"ticker","ask":"6387133","bid":"6386...1Z","volume":"1191.08"}' [179 bytes]
2024-02-06 00:12:16,945 - websockets.client - DEBUG - < TEXT '{"channel":"ticker","ask":"6387133","bid":"6384...3Z","volume":"1191.08"}' [179 bytes]
2024-02-06 00:12:16,946 - websockets.client - DEBUG - < TEXT '{"channel":"ticker","ask":"6387133","bid":"6384...3Z","volume":"1191.08"}' [179 bytes]
2024-02-06 00:12:17,990 - websockets.client - DEBUG - < TEXT '{"channel":"ticker","ask":"6386696","bid":"6384...6Z","volume":"1191.08"}' [179 bytes]
2024-02-06 00:12:22,499 - websockets.client - DEBUG - < TEXT '{"channel":"ticker","ask":"6386234","bid":"6384...4Z","volume":"1191.34"}' [179 bytes]
^C2024-02-06 00:12:23,126 - websockets.client - DEBUG - = connection is CLOSING
2024-02-06 00:12:23,127 - websockets.client - DEBUG - > CLOSE 1000 (OK) [2 bytes]
2024-02-06 00:12:23,145 - websockets.client - DEBUG - < CLOSE 1000 (OK) [2 bytes]
2024-02-06 00:12:23,146 - websockets.client - DEBUG - = connection is CLOSED
2024-02-06 00:12:23,146 - websockets.client - DEBUG - = connection is CLOSING
2024-02-06 00:12:23,146 - websockets.client - DEBUG - > CLOSE 1000 (OK) [2 bytes]
2024-02-06 00:12:23,161 - websockets.client - DEBUG - < CLOSE 1000 (OK) [2 bytes]
2024-02-06 00:12:23,161 - websockets.client - DEBUG - = connection is CLOSED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment