How to ping a site in Python?

Sometimes, we want to ping a site in Python.

In this article, we’ll look at how to ping a site in Python.

How to ping a site in Python?

To ping a site in Python, we can use the ping.verbose_ping method.

For instance, we write

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(e)

to call ping.verbose_ping to ping www.google.com 3 times.

And we ping www.wikipedia.com with

ping.Ping('www.wikipedia.org', timeout=2000).do()

And we time out the ping in 2 seconds by setting the timeout argument to 2000.

We call do to start the ping.

Conclusion

To ping a site in Python, we can use the ping.verbose_ping method.