Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ping program in Python
#11
Can change to this then DeaD_EyE code work on Windows to,if someone try to run there.
privileged = os.getlogin() == 0
A test of async_ping in icmplib and run in schedule way with use of schedule.
from icmplib import async_ping
import schedule
import time
import asyncio

async def is_alive(address):
    host = await async_ping(address, count=4, interval=0.2)
    if host.is_alive:
        print(f'{host.address} is up!')
        print(host.packets_received)
        print(host.packet_loss)
    else:
        print(f'{host.address} is down!')
        # Do something here

def check_run():
    return asyncio.run(is_alive('python-forum.io'))

schedule.every(10).seconds.do(check_run)
while True:
    schedule.run_pending()
    time.sleep(1)
Output:
λ python ping_sc.py 104.21.27.41 is up! 4 0.0 104.21.27.41 is up! 4 0.0 104.21.27.41 is up! 4 0.0 104.21.27.41 is up! 4 0.0
Reply
#12
what i wanted the ping program for is to modify it so that it would, every time it will send a ping, check how many replies it has received in the past N seconds and if that number is zero, run another script (or i could code that as a function to call).

i also want to set up the timing so that it does not accumulate slight errors in time.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#13
(Mar-04-2022, 08:27 PM)Skaperen Wrote: what i wanted the ping program for is to modify it so that it would, every time it will send a ping, check how many replies it has received in the past N seconds and if that number is zero, run another script (or i could code that as a function to call).
host.packets_received as you see my code dos that,then can just check if that is zero and do somthing.
(Mar-04-2022, 08:27 PM)Skaperen Wrote: i also want to set up the timing so that it does not accumulate slight errors in time.
There is timeout parameter(The maximum waiting time for receiving) default is 2,so can set to what you want.
Then with changes it will be like this.
from icmplib import async_ping
import schedule
import time
import asyncio

async def is_alive(address):
    host = await async_ping(address, count=4, interval=0.2, timeout=4)
    if host.packets_received ==  0:
        print(f'{host.address} is down!')
        # Call something
    else:
        print(f'{host.address} is up!')

def check_run():
    return asyncio.run(is_alive('python-forum.io'))

schedule.every(10).seconds.do(check_run)
while True:
    schedule.run_pending()
    time.sleep(1)
Output:
λ python ping_sc1.py 104.21.27.41 is up! 104.21.27.41 is up! 104.21.27.41 is up! 104.21.27.41 is up! 104.21.27.41 is up!
Reply
#14
i know what the system ping command has to do for its basic use. i wanted to see how that would be done in Python. in particular, i want to see how it waits for the arriving replies at any point in time and keeps on sending the ping requests whether any replies arrive or not. does it "cheat" and run 2 threads, one to send ping requests (up to how many requested) and one to receive replies (up to the proper time) and print the per-reply stats (duration of round-trip, etc).

then i would see how easy to have it track the state of reachability of the specified remote interface, with local actions to deal with it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  creapy ping Skaperen 0 1,798 Jul-04-2018, 02:55 AM
Last Post: Skaperen
  Can a python program execute an action on another software program? lex 4 3,984 Jan-26-2018, 03:10 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020