Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ping program in Python
#1
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).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Here is a simplified example using therequestslibrary.
import requests
from time import time

while True :
	start_time = time ()
	print (requests.get ('https://youtube.com'), end = ' ')
	print (f'{time () - start_time:.3f}')
Reply
#3
Did you search PyPI?
Reply
#4
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
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
Yeah, Requests does HTTP not ICMP.

What did you find on PyPI and what was hard about searching? You might also try searching GitHub.
Reply
#6
(Mar-03-2022, 05:30 AM)Skaperen Wrote: i searched PyPi but nothing looked promising. it's hard to search there
A 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))
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
Reply
#7
(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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(Mar-03-2022, 10:51 PM)Skaperen Wrote: 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?
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))
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 ------------------------------------------------------------
Reply
#9
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
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")
snippsat likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  creapy ping Skaperen 0 1,800 Jul-04-2018, 02:55 AM
Last Post: Skaperen
  Can a python program execute an action on another software program? lex 4 3,985 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