Server ping latency measurement - Python

I have a list of IP address servers, I need to check if each of them is on the network and for how long latency .

I have not found direct ways to implement this, and it seems that there are several problems in calculating the delays for sure.


Any ideas?

+5
source share
5 answers

If you are already comfortable working with syntax strings, you can use the subprocess module to get the data you are looking for into a string:

>>> import subprocess >>> p = subprocess.Popen(["ping.exe","www.google.com"], stdout = subprocess.PIPE) >>> print p.communicate()[0] Pinging www.l.google.com [209.85.225.99] with 32 bytes of data: Reply from 209.85.225.99: bytes=32 time=59ms TTL=52 Reply from 209.85.225.99: bytes=32 time=64ms TTL=52 Reply from 209.85.225.99: bytes=32 time=104ms TTL=52 Reply from 209.85.225.99: bytes=32 time=64ms TTL=52 Ping statistics for 209.85.225.99: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 59ms, Maximum = 104ms, Average = 72ms 
+5
source

If you want to avoid implementing all the information about network communication, you might try to create something on top of fping :

fping is a similar program that uses the Internet Control Message Protocol (ICMP) to determine if the target host is responding. Fping differs from ping in that you can specify any number of targets on the command line or specify a file containing lists of target pings. Instead of sending one target until it expires or responds, fping will send out a ping packet and go to the next target in circular mode.

+5
source

Following hlovdal's suggestion for working with fping , here is my solution that I use to test proxies. I just tried this on Linux. If ping time cannot be measured, a large value is returned. Usage: print get_ping_time('<ip>:<port>') .

 import shlex from subprocess import Popen, PIPE, STDOUT def get_simple_cmd_output(cmd, stderr=STDOUT): """ Execute a simple external command and get its output. """ args = shlex.split(cmd) return Popen(args, stdout=PIPE, stderr=stderr).communicate()[0] def get_ping_time(host): host = host.split(':')[0] cmd = "fping {host} -C 3 -q".format(host=host) res = [float(x) for x in get_simple_cmd_output(cmd).strip().split(':')[-1].split() if x != '-'] if len(res) > 0: return sum(res) / len(res) else: return 999999 
+3
source

https://github.com/matthieu-lapeyre/network-benchmark My solution based on the work of FlipperPA: https://github.com/FlipperPA/latency-tester

 import numpy import pexpect class WifiLatencyBenchmark(object): def __init__(self, ip): object.__init__(self) self.ip = ip self.interval = 0.5 ping_command = 'ping -i ' + str(self.interval) + ' ' + self.ip self.ping = pexpect.spawn(ping_command) self.ping.timeout = 1200 self.ping.readline() # init self.wifi_latency = [] self.wifi_timeout = 0 def run_test(self, n_test): for n in range(n_test): p = self.ping.readline() try: ping_time = float(p[p.find('time=') + 5:p.find(' ms')]) self.wifi_latency.append(ping_time) print 'test:', n + 1, '/', n_test, ', ping latency :', ping_time, 'ms' except: self.wifi_timeout = self.wifi_timeout + 1 print 'timeout' self.wifi_timeout = self.wifi_timeout / float(n_test) self.wifi_latency = numpy.array(self.wifi_delay) def get_results(self): print 'mean latency', numpy.mean(self.wifi_latency), 'ms' print 'std latency', numpy.std(self.wifi_latency), 'ms' print 'timeout', self.wifi_timeout * 100, '%' if __name__ == '__main__': ip = '192.168.0.1' n_test = 100 my_wifi = WifiLatencyBenchmark(ip) my_wifi.run_test(n_test) my_wifi.get_results() 

Github repository: https://github.com/matthieu-lapeyre/network-benchmark

+1
source

thanks from Jabba, but this code is not working correctly for me, so I am changing something like the following

 import shlex from subprocess import Popen, PIPE, STDOUT def get_simple_cmd_output(cmd, stderr=STDOUT): """ Execute a simple external command and get its output. """ args = shlex.split(cmd) return Popen(args, stdout=PIPE, stderr=stderr).communicate()[0] def get_ping_time(host): host = host.split(':')[0] cmd = "fping {host} -C 3 -q".format(host=host) # result = str(get_simple_cmd_output(cmd)).replace('\\','').split(':')[-1].split() if x != '-'] result = str(get_simple_cmd_output(cmd)).replace('\\', '').split(':')[-1].replace("n'", '').replace("-", '').replace( "b''", '').split() res = [float(x) for x in result] if len(res) > 0: return sum(res) / len(res) else: return 999999 def main(): # sample hard code for test host = 'google.com' print([host, get_ping_time(host)]) host = 'besparapp.com' print([host, get_ping_time(host)]) if __name__ == '__main__': main() 
0
source

Source: https://habr.com/ru/post/1305237/


All Articles