Link to home
Start Free TrialLog in
Avatar of summer_soccer
summer_soccer

asked on

what's wrong with my code to do concurrent traceroute to multiple IP addresses?

I am new to Python. When debugging the python code, I got the following error messages:

Exception in thread Thread-49:
Traceback (most recent call last):
  File "/usr/lib/python2.5/threading.py", line 486, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.5/threading.py", line 446, in run
    self.__target(*self.__args, **self.__kwargs)
TypeError: traceroute_worker() argument after * must be a sequence

The code is below:

Can anyone help me figure out what is wrong with the code?
    def traceroute_worker(self, q): 
        proc, tracefile = q.get() 
        while True: 
            if proc.poll() is not None: 
                map_traceroute(tracefile)
                q.task_done() 
                break 
            time.sleep(5) 
            
    def do_traceroute(self, peers): 
        sites = []
        for p in peers:
            sites.append(p[0][0])
        print sites
 
        q = Queue() 
        for i in range(50): 
            t = Thread(target=self.traceroute_worker, args=(q)) 
            t.setDaemon(True) 
            t.start() 
            
        for item in sites: 
            out = open('some-file', 'w') 
            q.put(Popen(['/usr/bin/traceroute', item], stdout=out))
                    
        q.join()       # block until all tasks are done

Open in new window

Avatar of ramrom
ramrom
Flag of United States of America image

Read the manual? "args is the argument tuple for the target invocation. Defaults to ()."

Try:

 t = Thread(target=self.traceroute_worker, args=(q,))
Avatar of summer_soccer
summer_soccer

ASKER

Thanks ranrom.

I changed to args=(q,), but now I got the error message:

in traceroute_worker
    proc, tracefile = q.get()
TypeError: 'Popen' object is not iterable


What is the cause of this error?
I changed code to below, and the error
TypeError: 'Popen' object is not iterable
is resolved.

But now I got another error:

in traceroute_worker
    time.sleep(5)
AttributeError: 'builtin_function_or_method' object has no attribute 'sleep'


I did do "from time import time" in the beginning. Do anybody know how this error is generated and how to resolve it?

Many thanks!

    def traceroute_worker(self, q): 
        proc = q.get() 
        tracefile = proc.stdout
        while True: 
            if proc.poll() is not None: 
                map_traceroute(tracefile)
                q.task_done() 
                break 
            time.sleep(5) 
            
    def do_traceroute(self, peers): 
        sites = []
        for p in peers:
            sites.append(p[0][0])
        print sites
 
        #t = SubnetTree.SubnetTree()
 
        q = Queue() 
        for i in range(50): 
            t = Thread(target=self.traceroute_worker, args= (q, )) 
            t.setDaemon(True) 
            t.start() 
            
        for item in sites: 
            out = open('some-file', 'w') 
            q.put(Popen(['/usr/bin/traceroute', item], stdout=out))
                    
        q.join()       # block until all tasks are done

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ramrom
ramrom
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial