Ping a site in Python?

前端 未结 15 2261
我寻月下人不归
我寻月下人不归 2020-11-22 09:22

How do I ping a website or IP address with Python?

15条回答
  •  灰色年华
    2020-11-22 09:56

    using subprocess ping command to ping decode it because the response is binary:

    import subprocess
    ping_response = subprocess.Popen(["ping", "-a", "google.com"], stdout=subprocess.PIPE).stdout.read()
    result = ping_response.decode('utf-8')
    print(result)
    

提交回复
热议问题