Gate WebSocket V4 Python SDK
Project description
Gate WebSocket Python SDK
gate_ws provides Gate WebSocket V4 Python implementation, including all channels defined in
spot(new) and futures WebSocket.
Features:
- Fully asynchronous
- Reconnect on connection to server lost, and resubscribe on connection recovered
- Support connecting to multiple websocket servers
- Highly configurable
Installation
This package requires Python version 3.6+. Python 2 will NOT be supported.
pip install --user gate-ws
Getting Started
import asyncio
from gate_ws import Configuration, Connection, WebSocketResponse
from gate_ws.spot import SpotPublicTradeChannel
# define your callback function on message received
def print_message(conn: Connection, response: WebSocketResponse):
if response.error:
print('error returned: ', response.error)
conn.close()
return
print(response.result)
async def main():
# initialize default connection, which connects to spot WebSocket V4
# it is recommended to use one conn to initialize multiple channels
conn = Connection(Configuration())
# subscribe to any channel you are interested into, with the callback function
channel = SpotPublicTradeChannel(conn, print_message)
channel.subscribe(["GT_USDT"])
# start the client
await conn.run()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Application Demos
We provide some demo applications in the examples directory, which can be run directly.
Advanced usage
- Subscribe to private channels
from gate_ws import Configuration, Connection from gate_ws.spot import SpotOrderChannel async def main(): conn = Connection(Configuration(api_key='YOUR_API_KEY', api_secret='YOUR_API_SECRET')) channel = SpotOrderChannel(conn, lambda c, r: print(r.result)) channel.subscribe(["GT_USDT"]) # start the client await conn.run()
- Your callback function can also be a coroutine
import asyncio async def my_callback(conn, response): await asyncio.sleep(1) print(response.result)
- You can provide a default callback function for all channels, so that when subscribing to new
channels, no additional callback function are needed.
from gate_ws import Configuration, Connection from gate_ws.spot import SpotPublicTradeChannel async def main(): # provide default callback for all channels conn = Connection(Configuration(default_callback=lambda c, r: print(r.result))) # default callback will be used if callback not provided when initializing channels channel = SpotPublicTradeChannel(conn) channel.subscribe(["GT_USDT"]) # start the client await conn.run()
- Subscribe to both spot and futures WebSockets
import asyncio from gate_ws import Configuration, Connection from gate_ws.spot import SpotPublicTradeChannel from gate_ws.futures import FuturesPublicTradeChannel async def main(): # initialize a spot connection, which is the default if no parameters is provided spot_conn = Connection(Configuration(app='spot')) # initialize a futures connection futures_conn = Connection(Configuration(app='futures', settle='usdt', test_net=False)) # subscribe to any channel you are interested into, with the callback function channel = SpotPublicTradeChannel(spot_conn, lambda c, r: print(r.result)) channel.subscribe(["BTC_USDT"]) channel = FuturesPublicTradeChannel(futures_conn, lambda c, r: print(r.result)) channel.subscribe(["BTC_USDT"]) # start both connection await asyncio.gather(spot_conn.run(), futures_conn.run())
- You can use your own executor pool to run your callback function
import concurrent.futures from gate_ws import Configuration, Connection from gate_ws.spot import SpotPublicTradeChannel async def main(): # use process pool to run your callback function with concurrent.futures.ProcessPoolExecutor() as pool: conn = Connection(Configuration(executor_pool=pool)) # default callback will be used if callback not provided when subscribing channel = SpotPublicTradeChannel(conn, lambda c, r: print(r.result)) channel.subscribe(["GT_USDT"]) # start the client await conn.run()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gate_ws-0.5.6.tar.gz.
File metadata
- Download URL: gate_ws-0.5.6.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f8dba302b65e70a2234928f2ac8d1c1c7a14025481cf691c6ef4c3e84b51152
|
|
| MD5 |
08cf556cf817b85e94a4a5447ad3c5e1
|
|
| BLAKE2b-256 |
1341d2242b9c9eec49cd2f726258361d0ca5cefa8352997012bd66647126879a
|
Provenance
The following attestation bundles were made for gate_ws-0.5.6.tar.gz:
Publisher:
workflow.yml on gate/gatews
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gate_ws-0.5.6.tar.gz -
Subject digest:
5f8dba302b65e70a2234928f2ac8d1c1c7a14025481cf691c6ef4c3e84b51152 - Sigstore transparency entry: 778962821
- Sigstore integration time:
-
Permalink:
gate/gatews@98c595c5d94674f376f5a69523ec524e3fcf1cf0 -
Branch / Tag:
refs/tags/v0.5.6 - Owner: https://github.com/gate
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@98c595c5d94674f376f5a69523ec524e3fcf1cf0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file gate_ws-0.5.6-py3-none-any.whl.
File metadata
- Download URL: gate_ws-0.5.6-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d9323e9c603f15e205889d1923e4df4e412465c1452264473cfea03586da3916
|
|
| MD5 |
1e4196721172507fa05dd354e0634fbf
|
|
| BLAKE2b-256 |
94c52cf6a1de6d62e97216ae18839126554c948591689f5a511f77469e6807ba
|
Provenance
The following attestation bundles were made for gate_ws-0.5.6-py3-none-any.whl:
Publisher:
workflow.yml on gate/gatews
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gate_ws-0.5.6-py3-none-any.whl -
Subject digest:
d9323e9c603f15e205889d1923e4df4e412465c1452264473cfea03586da3916 - Sigstore transparency entry: 778962828
- Sigstore integration time:
-
Permalink:
gate/gatews@98c595c5d94674f376f5a69523ec524e3fcf1cf0 -
Branch / Tag:
refs/tags/v0.5.6 - Owner: https://github.com/gate
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@98c595c5d94674f376f5a69523ec524e3fcf1cf0 -
Trigger Event:
push
-
Statement type: