---
# System prepended metadata

title: '[python wss]MAX交易所websocket'

---

MAX交易所WSS連接方式

https://max.maicoin.com/documents/api
https://maicoin.github.io/max-websocket-docs/#/public_channels?id=subscribe


```
import websocket
import json


def on_open(_wsa):
    data = {
        "action": "sub",
        "subscriptions": [
            {"channel": "book", "market": "btctwd", "depth": 1},
            {"channel": "trade", "market": "btctwd"},
        ],
        "id": "client1",
    }
    _wsa.send(json.dumps(data))


def on_message(_wsa, message):
    print(message, "\n")


def hello():
    print("hello")
    wss = "wss://max-stream.maicoin.com/ws"
    wsa = websocket.WebSocketApp(wss, on_message=on_message, on_open=on_open)
    wsa.run_forever()


if __name__ == "__main__":
    hello()

```