![]() |
ping program in Python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: News and Discussions (https://python-forum.io/forum-31.html) +--- Thread: ping program in Python (/thread-36547.html) Pages:
1
2
|
ping program in Python - Skaperen - Mar-02-2022 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). RE: ping program in Python - BashBedlam - Mar-03-2022 Here is a simplified example using the 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}') RE: ping program in Python - ndc85430 - Mar-03-2022 Did you search PyPI? RE: ping program in Python - Skaperen - Mar-03-2022 i don't understand how that requests example sends and receives a ping packet. i searched PyPi but nothing looked promising. it's hard to search there RE: ping program in Python - ndc85430 - Mar-03-2022 Yeah, Requests does HTTP not ICMP. What did you find on PyPI and what was hard about searching? You might also try searching GitHub. RE: ping program in Python - snippsat - Mar-03-2022 (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. These took me not long time to find,can also test all. icmplib | pythonping | ping3. icmplib(GitHub) maybe the best one a modern implementation of the ICMP protocol in Python. from icmplib import ping, multiping, traceroute, resolve print(ping('google.com', count=4)) 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')
RE: ping program in Python - Skaperen - Mar-03-2022 (Mar-03-2022, 03:56 PM)snippsat Wrote: The rest.does that mean to concatenate the two source blocks? i assume yes ... there are 2 imports bringing in the name "ping" at line 1-1 and line 2-2. so, which should i use? RE: ping program in Python - snippsat - Mar-03-2022 (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. After looking at this the clear best implantation is icmplib,and what i would advice to use. from icmplib import ping print(ping('google.com', count=4))
RE: ping program in Python - Skaperen - Mar-04-2022 i want my code to run while the ping is on its way there and the reply is on its way back. i need to send a ping request every second, wake up in X seconds and check if anything was received. if not, run the fixer script (i'm thinking it should wait for it). edit 1: so, i need a means to send pings separate, then i need a means to get what comes back when it does, separate from other traffic from the same host (i may be pinging it manually in some virtual terminal. RE: ping program in Python - DeaD_EyE - Mar-04-2022 Cool, you can use it as unprivileged user. 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") |