Ping a site in Python?

user40572 picture user40572 · Nov 25, 2008 · Viewed 226.4k times · Source

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

Answer

orip picture orip · Nov 25, 2008

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.