如何包装一个将ping主机超时的python脚本?

如何包装一个将ping主机超时的python脚本?,python,linux,ping,Python,Linux,Ping,我想创建一个python3脚本,它将接受超时、接口和主机名作为参数,并实时打印输出。设置超时和实时打印响应时遇到问题 def check_ping(): hostname = "google.com" response = os.system("ping -c 1 " + hostname) # and then check the response... if response == 0: pingstat

我想创建一个python3脚本,它将接受超时、接口和主机名作为参数,并实时打印输出。设置超时和实时打印响应时遇到问题

def check_ping():
    hostname = "google.com"
    response = os.system("ping -c 1 " + hostname)
    # and then check the response...
    if response == 0:
        pingstatus = "Network Active"
    else:
        pingstatus = "Network Error"

    return pingstatus
我认为在调用check_ping()作为超时之前,可以使用sleep()函数!!