ping multiple Ips with python

Andrew McGregor andrew at indranet.co.nz
Mon Jan 6 19:45:42 EST 2003


Well, there's nothing in there to cause it to exit.  Two ways to fix it:

1) Make all the pingers daemon threads, then have the main thread exit when 
you're done.

2) Like you said, have each one exit when it's done.

You should maybe try 5 or so times before declaring a host down, many hosts 
rate limit ICMP responses globally, so if they're heavily loaded they won't 
answer pings reliably.

Andrew

--On Monday, January 06, 2003 15:45:07 -0800 ".d.hos" 
<dhostetler at sopris.net> wrote:

> Andrew McGregor <andrew at indranet.co.nz> wrote in message
> news:<mailman.1041767229.6505.python-list at python.org>...
>> I wasn't sure at a first glance if it did work.  Now I go back and look,
>> I'm sure it does.
>>
>> Andrew
>>
>> --On Sunday, January 05, 2003 00:21:07 +0000 darrell
>> <dgallion1 at yahoo.com>  wrote:
>>
>> > Didn't mess with the checksum since it works and it's fast enough.
>> > The pyrex code was cool to look at.
>>
>> Thanks.  I was just trying to learn pyrex, but since it's also around
>> 30x  faster, I'm going to keep it :-)
>>
>> Andrew
>
>
> Andrew,
> Thanks for your response and suggestions - although i'm still having
> issues with the code.
>
> *although "p" vs. "P" did get rid of the intital error.
>
> I looked into other multithreaded examples on the web and came up with
> the following:
>
> =============================
> import threading, os
>
> def log(s):
>     print s
>
> class Pinger(threading.Thread):
>     def __init__(self, address, *args, **kwargs):
>         self.address = address
>         threading.Thread.__init__(self, *args, **kwargs)
>
>     def run(self):
>         pingCard = "ping -n 10 " + self.address
>         child = os.popen(pingCard).readlines()
>         log(child)
>
>
> if __name__ == '__main__':
>
>     PingList = ["127.0.0.1", "66.218.71.83", "64.66.148.171"]
>     threadList = []
>
>     # Create threads
>     for i in PingList :
>         threadList.append(Pinger(i))
>
>     # Start all threads
>     for thread in threadList:
>         thread.start()
> ============================================
>
> when run through idle, the python shell hangs after all the ips
> respond. Do I have to stop each thread individually after it executes
> the ping command? other than that it seems to work....
>
> thanks again
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>







More information about the Python-list mailing list