Ping a site in Python?


Question

How do I ping a website or IP address with Python?

1
71
6/24/2019 6:52:31 PM

See this pure Python ping by Matthew Dixon Cowles and Jens Diemer. Also, remember that Python requires root to spawn ICMP (i.e. ping) sockets in linux.

import ping, socket
try:
    ping.verbose_ping('www.google.com', count=3)
    delay = ping.Ping('www.wikipedia.org', timeout=2000).do()
except socket.error, e:
    print "Ping Error:", e

The source code itself is easy to read, see the implementations of verbose_ping and of Ping.do for inspiration.

77
12/5/2014 1:31:43 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon