Python Function to test ping

前端 未结 5 1509
既然无缘
既然无缘 2020-12-09 08:59

I\'m trying to create a function that I can call on a timed basis to check for good ping and return the result so I can update the on-screen display. I am new to python so I

5条回答
  •  旧巷少年郎
    2020-12-09 09:35

    Adding on to the other answers, you can check the OS and decide whether to use "-c" or "-n":

    import os, platform
    host = "8.8.8.8"
    os.system("ping " + ("-n 1 " if  platform.system().lower()=="windows" else "-c 1 ") + host)
    

    This will work on Windows, OS X, and Linux

    You can also use sys:

    import os, sys
    host = "8.8.8.8"
    os.system("ping " + ("-n 1 " if  sys.platform().lower()=="win32" else "-c 1 ") + host)
    

提交回复
热议问题