import subprocess try: subprocess.call("ping -t localhost", timeout=3) except TimeoutError: print("Timeout happened\n")