编辑代码

import subprocess
from platform import system

def ping(host):
    param = "-n" if system().strip().lower() == "windows" else "-c"
    command = ["ping", param, "1", host]
    try:
        result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
        return True
    except subprocess.CalledProcessError as e:
        print(f"Command failed with return code {e.returncode}")
        return False
    except Exception as e:
        print(f"An error occurred while running the command: {e}")
        return False

if __name__ == "__main__":
    print(ping("www.jsrun.net"))