Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Appending a list in a class from a callback function
Hi all,
This is my first post. Usually I can search out answers to my problems but today I'm having a bit of difficulty finding what I'm doing wrong populating a list of MAC addresses from a callback function, all enclosed within a class. The list is always empty on the very last line where I print it out. If I print the contents of the list from within the callback function each time it is raised, it looks correct. I'm having a feeling it has to do with scope or might need a lambda function but I'm foggy on some aspects there. I apologize for my numerous un-pythonic statements. I'm totally self taught and come from using mainly Visual Basic so I can be a bit of a sloppy coder at times. Here's my code: NOTE: This is normally executed on a Raspberry Pi.

import nmap, sys, socket, subprocess

class IP_Roam():
    def __init__(self, root_ip="", display_on=False):
        self.mac_list = []
        self.display_on = display_on
        self.router_ip = root_ip
        self.end_ip = ""

    def callback_result(self, host, scan_result):
        if self.display_on:
            sys.stdout.write('\rScanning ' + str(host).ljust(15, ' '))

        for ip in scan_result['scan']:

                mac = scan_result['scan'][u'' + str(ip)]['addresses'][u'mac']
                if mac:
                #ipv4 = scan_result['scan'][u'' + str(ip)]['addresses'][u'ipv4']
                # This happens when it finds itself

    def go(self):
        if self.router_ip == "":
        nma = nmap.PortScannerAsync()
        nma.scan(self.router_ip + "/24", arguments='-sP', callback=self.callback_result)

        while nma.still_scanning():

        if self.display_on:
            for mac in self.mac_list:

    def who_am_i(self, ipend=None):
        my_ips = subprocess.check_output(("hostname", "-I"), stderr=None).decode('utf-8').split(" ")
        nodes = my_ips[0].split(".")
        self.router_ip = str(nodes[0]) + "." + str(nodes[1]) + ".1.0"

        if ipend is not None:
            self.end_ip = str(nodes[0]) + "." + str(nodes[1]) + ".1." + str(ipend)
            if self.display_on:
                print("Using: " + self.router_ip + "-" + self.end_ip)
            if self.display_on:
                print("Using: " + self.router_ip)

if __name__ == "__main__":
    myscan = IP_Roam(display_on=True)
need to be
Actually, I originally had it that way with the same result. I changed it in hopes that it'd resolve the issue.
I just changed it back to
with no change. mac_list is still not populated when IP_Roam.go() completes.
Having a try/except that excepts every exception and pass when there is an exception, will silently let any exception go by.
I've simplified my question a bit here to see if anyone can help me with this. When I run the following IP Scan, the results print fine from within the callback function. The whole list will even appear if I do a print() of self.mac_list from within the callback function. However, when the routine terminates and go() returns self.mac_list it is always empty. Anyone know what I am missing here?

import nmap

class IP_Roam():
    def __init__(self, root_ip="", display_on=False):
        self.mac_list = []

    def callback_result(host, scan_result):

    def go():
        nma = nmap.PortScannerAsync()
        nma.scan(hosts='', arguments='-sP', callback=callback_result)

        while nma.still_scanning():
            print("Waiting >>>")
            nma.wait(2)   # you can do whatever you want but I choose to wait after the end of the scan
        return self.mac_list

if __name__ == "__main__":
    ps = IP_Roam()
    results = ps.go()

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  update imhow in callback from pyaudio markB 0 38 May-28-2020, 06:01 PM
Last Post: markB
  appending list of list glennford49 2 216 Mar-29-2020, 09:33 AM
Last Post: ibreeden
  Using input function in a Class wew044 2 251 Feb-06-2020, 03:08 AM
Last Post: wew044
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 430 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Appending to list not working and causing a infinite loop eiger23 8 445 Oct-10-2019, 03:41 PM
Last Post: eiger23
  how to add class instance attributes from list 999masks 2 382 Jul-22-2019, 07:59 AM
Last Post: 999masks
  How to call base class function kamal_chennai 1 467 Apr-03-2019, 01:12 PM
Last Post: ichabod801
  Build class to make a Smart list - trying icm63 7 598 Mar-28-2019, 08:53 PM
Last Post: icm63
  "not defined" error in function referencing a class Exsul 2 768 Mar-27-2019, 11:59 PM
Last Post: Exsul
  Why Can't I call base class function for my derived class object? AmirAB 2 544 Feb-13-2019, 03:04 PM
Last Post: snippsat

Forum Jump:

Users browsing this thread: 1 Guest(s)