How to ping a server/website with windows python?

11,909

The simplest way is to use os.system. In Windows:

import os
def ping(address):
    return not os.system('ping %s -n 1' % (address,))

Windows' ping returns 0 on success and 1 on failure, so we need to not the result.

If you want to suppress ping's output, simply add > NUL to the command.

Share:
11,909
Ashwin Gupta
Author by

Ashwin Gupta

I'm a high school student.

Updated on June 04, 2022

Comments

  • Ashwin Gupta
    Ashwin Gupta almost 2 years

    Like the title says, Im trying to ping a website (Like google.com) with python in windows. All I want the program to do is return True if the ping was received, or False if it wasn't. What is the absolutely most simple way to do this?