samuel / python-ping

Pure Python version of ICMP ping
229 stars 309 forks source link

it won't get the right result when use multithread to ping #1

Open kwanhur opened 10 years ago

kwanhur commented 10 years ago

make a flag in the header packet and then received data by select.select, we can judge the data whether it's the right one.

roeften commented 8 years ago

The issue is that the reply code is not checked, success is reported on any reply

a start would be to actually check the code:

index 7592db0..2f09a79 100644
--- a/gping.py
+++ b/gping.py
@@ -204,7 +204,12 @@ class GPing:
                 self.pings[packet_id]['delay'] = time_received - time_sent

                 # i'd call that a success
-                self.pings[packet_id]['success'] = True
+                if code == 0:
+                    self.pings[packet_id]['success'] = True
+                else
+                    self.pings[packet_id]['success'] = False
+
+                self.pings[packet_id]['code'] = code

                 # call our callback if we've got one
                 self.pings[packet_id]['callback'](self.pings[packet_id])

patch.txt

kyan001 commented 8 years ago

I met this problem too.

The reason that this happened is "os.getpid()" returns the same number while multithreading. os.getpid() returns different number while multiprocessing.

the way to solve this is replace "my_ID = os.getpid() & 0xFFFF" by "my_ID = threading.current_thread().ident & 0xFFFF"(py3) in do_one() function.

I'll fix this in ping3.py and ask for a merge. before the pull request works, see my branch @ https://github.com/kyan001/python-ping