Nez
Nez

Reputation: 39

Websockets working on local machine but not on cloud

This is a program to grab data from Binance mini ticker and store it into a DB. The program runs fine on my local machine, but when I upload it anywhere online, it gives me an error.

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()

I usually run it using a separate handler program, but even so the same error occurs.

Error:

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)

Upvotes: 0

Views: 580

Answers (1)

cborch
cborch

Reputation: 41

Your problem is the self argument in on_open(). on_open() only gets the ws argument so the inclusion of self is causing the error. I understand that you're passing self because you are trying to define a class.

If you need to keep the class implementation, you should be able to use lambda functions as demonstrated in this answer. However, you don't reference self in on_open() so you might consider whether you really need this to be a class.

Upvotes: 2

Related Questions