Alex L Alex L - 1 year ago 214
Python Question

Python ICMP ping implementation when pinging multiple ips from threads?

I've been using jedie's python

implementation on Windows. I could be wrong, but when pinging two computers (A and B) from separate threads, ping will return the first ping it receives, regardless of source.

Since it could be an issue with jedie's fork, I reverted to the previous version. (This is the version I'm going to explore below)

I added in a line of code in
: (Line 134 or similar)

recPacket, addr = my_socket.recvfrom(1024) # Existing line
print "dest: {}, recv addr: {}.".format(dest_addr, addr) # New line

This allows us to see the address of the ping we're receiving. (Should be same as the destination IP, right?)


ping1() pings a known offline IP (,

ping2() pings a known online IP ( - my router)

>>> from ping import do_one

>>> def ping1():
print "Offline:", do_one("",1)

>>> ping1()
Offline: None

>>> def ping2():
print "Online:", do_one("",1)

>>> ping2()
Online: dest:, recv addr: ('', 0).

Now if we do them together: (Using Timer for simplicity)

>>> from threading import Timer
>>> t1 = Timer(1, ping1)
>>> t2 = Timer(1, ping2)
>>> t1.start(); t2.start()
>>> Offline:Online: dest:, recv addr: ('', 0).dest:, recv addr: ('', 0).


It's a little mangled (due to print not working nicely with threading), so here it is a bit clearer:

>>> Online: dest:, recv addr: ('', 0).
Offline:dest:, recv addr: ('', 0). # this is the issue - I assume dest should be the same as recv address?


My questions:

  1. Can anyone recreate this?

  2. Should ping be behaving like this? I assume not.

  3. Is there an existing ICMP ping for python that will not have this behaviour?

    Alternatively, can you think of an easy fix - ie polling
    until our destination matches our receive address?

Edit: I've created an issue on the python-ping github page

Answer Source

This is happening because of the nature of ICMP. ICMP has no concept of ports, so all ICMP messages are received by all listeners.

The usual way to disambiguate is to set a unique identifier in the ICMP ECHO REQUEST payload, and look for it in the response. This code appears to do that, but it uses the current process id to compose the ID. Since this is multithreaded code, they will share a process id and all listeners in the current process will think all ECHO REPLYs are ones they themselves sent!

You need to change the ID variable in do_one() so that it is per-thread unique. You will need to change this line in do_one():

my_ID = os.getpid() & 0xFFFF

Possibly this will work as an alternative, but ideally you should use a real 16-bit hashing function:

# add to module header
    from thread import get_ident
except ImportError:
        from _thread import get_ident
    except ImportError:
        def get_ident():
            return 0

# now in do_one() body:
my_ID = (get_ident() ^ os.getpid()) & 0xFFFF

I don't know if this module has any other thread issues, but it seems to be ok from a cursory examination.

Using the jedie implementation, you would make a similar change for the Ping() own_id constructor argument. You can either pass in an id you know to be unique (like above) and manage Ping() objects yourself, or you can change this line (110) in the constructor:

self.own_id = os.getpid() & 0xFFFF

Also see this question and answer and answer comment thread for more info.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download