Python Forum
Appending a list in a class from a callback function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Appending a list in a class from a callback function
#6
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?

Thanks,
Jim
import nmap

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

    def callback_result(host, scan_result):
        print(scan_result)
        self.mac_list.append(scan_result)

    def go():
        nma = nmap.PortScannerAsync()
        nma.scan(hosts='192.168.1.0/30', 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()
    print(results)
Reply


Messages In This Thread
RE: Appending a list in a class from a callback function - by snizbatch - Sep-01-2019, 06:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 738 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  How to read module/class from list of strings? popular_dog 1 496 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  TimeOut a function in a class ? Armandito 1 1,673 Apr-25-2022, 04:51 PM
Last Post: Gribouillis
  Calling a class from a function jc4d 5 1,857 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  a function common to methods of a class Skaperen 7 2,664 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,942 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Tuple generator, and function/class syntax quazirfan 3 3,934 Aug-10-2021, 09:32 AM
Last Post: buran
  Time.sleep: stop appending item to the list if time is early quest 0 1,890 Apr-13-2021, 11:44 AM
Last Post: quest
  Reading and appending list MrSwiss 1 1,745 Mar-01-2021, 09:01 AM
Last Post: Serafim
  apendng to a list within a class gr3yali3n 4 2,378 Feb-16-2021, 06:30 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020