Skip to content

Instantly share code, notes, and snippets.

@atitawat-pol
Created September 11, 2022 14:26
Show Gist options
  • Save atitawat-pol/d7e5c91bbf1158805f1a17378eb99a3c to your computer and use it in GitHub Desktop.
Save atitawat-pol/d7e5c91bbf1158805f1a17378eb99a3c to your computer and use it in GitHub Desktop.
Real-Time Cryptocurrencies Prices from FTX/Binance using Python
def test_connection():
import websocket
import json
# What to do when opening a new connection
def on_open(self):
# Prepare sending message
sub = json.dumps({
"op": "subscribe",
"channel": "trades",
"market": "BTC/USD"
})
# Send information to the server
self.send(sub)
print("subscribed")
# What to do when receiving a message from server
# Our action will be placed here
def on_message(self, message):
print(message)
# What to do when closing a connection
def on_close(self):
print("### close ###")
# websocket logging
websocket.enableTrace(True)
# websocket object creation
wsapp = websocket.WebSocketApp(
"wss://ftx.com/ws/",
on_open=on_open,
on_message=on_message,
on_close=on_close
)
# Run the websocket client
wsapp.run_forever()
if __name__ == '__main__':
test_connection()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment