这是一个从Binance滴答器获取数据并将其存储到DB中的程序。这个程序在我的本地机器上运行的很好,但是当我把它上传到任何一个在线的地方时,它会给我一个错误。
import websocket
import pandas as pd
import rel
import datetime
from data import Database
class Binance:
def __init__(self):
self.uri = "wss://stream.binance.com"
self.markets = ['bnbusdt@miniTicker', 'btcusdt@miniTicker']
self.stream = '/'.join(self.markets)
self.path = "Binance.db"
self.db = Database(self.path)
def push(self, res, db):
res = eval(res)
type(res)
data = res['data']
data.pop('e')
df = pd.DataFrame([data])
df.to_sql(res['stream'].split('@', 1)[0],
con=db.connection,
if_exists='append')
def on_message(self, ws, message):
print(message)
self.push(message, self.db)
def on_error(self, ws, error):
print(error)
with open("Binance.txt", 'a') as f:
error = error + " " + str(datetime.datetime.now()) + "\n"
f.write(error)
def on_close(self, ws, close_status_code, close_msg):
print("### closed ###")
with open("Binance.txt", 'a') as f:
message = close_msg + " " + str(datetime.datetime.now()) + "\n"
f.write(message)
self.start()
def on_open(self, ws):
print("Opened connection")
def start(self):
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://stream.binance.com/stream?streams=" + self.stream,
on_open=self.on_open,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close)
ws.run_forever(dispatcher=rel) # Set dispatcher to automatic reconnection
rel.signal(2, rel.abort) # Keyboard Interrupt
rel.dispatch()
binance = Binance()
binance.start()我通常使用一个单独的处理程序来运行它,但是即使这样,也会发生同样的错误。
错误:
error from callback <bound method Binance.on_open of <__main__.Binance object at 0x7f3562ba0910>>: on_open() missing 1 required positional argument: 'ws'
File "/usr/local/lib/python3.9/site-packages/websocket/_app.py", line 344, in _callback
callback(*args)发布于 2022-05-27 13:43:49
您的问题是on_open()中的自论证。on_open()只获取ws参数,因此包含self将导致错误。我知道你是在传递自我,因为你试图定义一个类。
如果您需要保留类实现,您应该能够使用lambda函数,如this answer中所示。但是,您没有在on_open()中引用self,所以您可能会考虑是否真的需要这个类。
https://stackoverflow.com/questions/72393455
复制相似问题