0

When I run the binance.py file, I get an error like the following. I am sending data in class. How do I fix this?

import websockets
from binance.client import Client
from binance.websockets import BinanceSocketManager


class BinanceFutures(object):
    print("binance futures class")
    # websocket start
    futures_api_key = "oooo"
    futures_secret_key = "ppppp"
    client = Client(futures_api_key, futures_secret_key)
    bm = BinanceSocketManager(client)
    conn_key = bm.start_trade_socket('BNBBTC', process_message)
    bm.start()


    def process_message(self, msg):
        print("message type: {}".format(msg['e']))
        print(msg)
        # do something


if __name__ == "__main__":
    print("binance main")

The error I got:

Traceback (most recent call last):
  File "binancetest.py", line 6, in <module>
    class BinanceFutures:
  File "binancetest.py", line 13, in BinanceFutures
    conn_key = bm.start_trade_socket('BNBBTC', process_message)
NameError: name 'process_message' is not defined

martineau
  • 119,623
  • 25
  • 170
  • 301
doktorcuk fatih
  • 85
  • 1
  • 1
  • 4
  • 1
    As you defined `process_message` inside your class, you should refer to it as `self.process_message` instead. – Ralubrusto Oct 31 '20 at 17:05

2 Answers2

0

It's saying, that you haven't defined the name process_message, if you define it somewhere in the class or in the file, the error will stop

Oliver Hnat
  • 797
  • 4
  • 19
0

You are not defining nor using a Python class properly.

I think you need to do it like this (untested). I've added an __init__() method to initialize the class when an instance of it is created. In particular note how I added self. prefix to the self.process_message argument being passed to bm.start_trade_socket() since it's a method of the class.

You may need to make bm an instance attribute, too. (i.e. self.bm everywhere it's referenced.)

import websockets
from binance.client import Client
from binance.websockets import BinanceSocketManager


class BinanceFutures:
    def __init__(self):
        print("binance futures class")
        # websocket start
        futures_api_key = "oooo"
        futures_secret_key = "ppppp"
        client = Client(futures_api_key, futures_secret_key)
        bm = BinanceSocketManager(client)
        conn_key = bm.start_trade_socket('BNBBTC', self.process_message)
        bm.start()

    def process_message(self, msg):
        print("message type: {}".format(msg['e']))
        print(msg)
        # do something


if __name__ == "__main__":
    print("binance main")
martineau
  • 119,623
  • 25
  • 170
  • 301