Measuring ping latency of a server - Python

后端 未结 5 443
情话喂你
情话喂你 2020-12-31 20:14

I have a list of server IP addresses, I need to check if each one is online and how long the latency is.

I haven\'t found any strai

5条回答
  •  耶瑟儿~
    2020-12-31 21:05

    thanks from Jabba but that code doesn't work correctly for me so i change something like following

    import shlex
    from subprocess import Popen, PIPE, STDOUT
    
    
    def get_simple_cmd_output(cmd, stderr=STDOUT):
        """
        Execute a simple external command and get its output.
        """
        args = shlex.split(cmd)
        return Popen(args, stdout=PIPE, stderr=stderr).communicate()[0]
    
    
    def get_ping_time(host):
        host = host.split(':')[0]
        cmd = "fping {host} -C 3 -q".format(host=host)
        # result = str(get_simple_cmd_output(cmd)).replace('\\','').split(':')[-1].split() if x != '-']
        result = str(get_simple_cmd_output(cmd)).replace('\\', '').split(':')[-1].replace("n'", '').replace("-",
                                                                                                            '').replace(
            "b''", '').split()
        res = [float(x) for x in result]
        if len(res) > 0:
            return sum(res) / len(res)
        else:
            return 999999
    
    
    def main():
        # sample hard code for test
        host = 'google.com'
        print([host, get_ping_time(host)])
    
        host = 'besparapp.com'
        print([host, get_ping_time(host)])
    
    
    
    if __name__ == '__main__':
        main()
    

提交回复
热议问题