Ashwin Gupta
Ashwin Gupta

Reputation: 2197

How to ping a server/website with windows python?

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?

Upvotes: 2

Views: 15206

Answers (1)

SwedishOwlSerpent
SwedishOwlSerpent

Reputation: 355

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.

Upvotes: 8

Related Questions