连接 Bitstamp websocket 后如何返回数据供全局使用?
我已成功订阅 Bitstamp websocket,现在正在接收实时交换数据。一旦我解析了这些数据并将其分配给一个变量(例如“asks”),如何返回该变量以便我可以在其他函数和其他 .py 文件中使用它?
或者我必须在 on_message() 函数中完成所有操作吗?非常感谢,代码如下:
import json
import requests
import websocket
SOCKET = "wss://ws.bitstamp.net/"
def on_open(ws):
print("opened connection")
subscription = {
"event": "bts:subscribe",
"data": {
"channel": "order_book_btcusd"
}
}
ws.send(json.dumps(subscription))
def on_close(ws):
print("closed connection")
def on_message(ws,message):
print(message)
data = json.loads(message)['data']
asks = float(bitstamp_data['asks'][0][0])
def on_error(ws,error):
print(error)
ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close, on_message=on_message,on_error=on_error)
ws.run_forever()
I've successfully subscribed to the Bitstamp websocket and I'm now receiving live exchange data. Once I've parsed this data and assigned it to a variable (eg "asks"), how do I return this variable so that I can use it in other functions and other .py files?
Or do I have to do everything within the on_message() function? Thanks very much, code below:
import json
import requests
import websocket
SOCKET = "wss://ws.bitstamp.net/"
def on_open(ws):
print("opened connection")
subscription = {
"event": "bts:subscribe",
"data": {
"channel": "order_book_btcusd"
}
}
ws.send(json.dumps(subscription))
def on_close(ws):
print("closed connection")
def on_message(ws,message):
print(message)
data = json.loads(message)['data']
asks = float(bitstamp_data['asks'][0][0])
def on_error(ws,error):
print(error)
ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close, on_message=on_message,on_error=on_error)
ws.run_forever()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论