Mar-02-2022, 11:17 PM
i am looking for a Python3 implementation of the ping program. i've already done one that invokes /bin/ping and it is too awkward to do what i want (timing changes for the ping packets).
requests
library.import requests from time import time while True : start_time = time () print (requests.get ('https://youtube.com'), end = ' ') print (f'{time () - start_time:.3f}')
(Mar-03-2022, 05:30 AM)Skaperen Wrote: [ -> ]i searched PyPi but nothing looked promising. it's hard to search thereA standard search eg google you should be able to find something.
from icmplib import ping, multiping, traceroute, resolve print(ping('google.com', count=4))
Output: 142.250.74.142
------------------------------------------------------------
Packets sent: 4
Packets received: 4
Packet loss: 0.0%
Round-trip times: 44.0 ms / 46.749 ms / 51.999 ms
Jitter: 2.666 ms
------------------------------------------------------------
The rest.import subprocess from pythonping import ping from ping3 import verbose_ping # The one on OS output = subprocess.run(['ping', 'google.com'], capture_output=True, encoding='utf-8') print(output.stdout) print('-' * 50) # pip install pythonping ping('google.com', verbose=True) print('-' * 50) # pip install ping3 verbose_ping('google.com')
Output:Pinging google.com [142.250.74.142] with 32 bytes of data:
Reply from 142.250.74.142: bytes=32 time=39ms TTL=50
Reply from 142.250.74.142: bytes=32 time=45ms TTL=50
Reply from 142.250.74.142: bytes=32 time=50ms TTL=50
Reply from 142.250.74.142: bytes=32 time=46ms TTL=50
Ping statistics for 142.250.74.142:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 39ms, Maximum = 50ms, Average = 45ms
--------------------------------------------------
Reply from 142.250.74.142, 29 bytes in 44.09ms
Reply from 142.250.74.142, 29 bytes in 39.87ms
Reply from 142.250.74.142, 29 bytes in 39.8ms
Reply from 142.250.74.142, 29 bytes in 39.8ms
--------------------------------------------------
ping 'google.com' ... 39ms
ping 'google.com' ... 39ms
ping 'google.com' ... 39ms
ping 'google.com' ... 39ms
(Mar-03-2022, 03:56 PM)snippsat Wrote: [ -> ]The rest.does that mean to concatenate the two source blocks? i assume yes ...
(Mar-03-2022, 10:51 PM)Skaperen Wrote: [ -> ]does that mean to concatenate the two source blocks? i assume yes ...The last code is just 3 different solution's run at once,just spilt it up want to use one them.
there are 2 imports bringing in the name "ping" at line 1-1 and line 2-2. so, which should i use?
from icmplib import ping print(ping('google.com', count=4))
Output: 142.250.74.142
------------------------------------------------------------
Packets sent: 4
Packets received: 4
Packet loss: 0.0%
Round-trip times: 44.0 ms / 46.749 ms / 51.999 ms
Jitter: 2.666 ms
------------------------------------------------------------
import os import time import icmplib def pinger(host, callback=None, timeout=1, *cb_args, **cb_kwags): def ping(): privileged = os.getuid() == 0 return icmplib.ping( host, count=1, timeout=timeout, privileged=privileged ).is_alive while True: time.sleep(1) if not ping(): print(host, "is dead") if callable(callback): callback(*cb_args, **cb_kwargs) else: print(host, "is alive") pinger("google.de")