Bottle Python ping check 5 pings
我正在尝试编写一个脚本,用5次ping向浏览器打印ping检查。
这个脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from bottle import route, run, template import subprocess @route('/pings/<ip>') def ping(ip): param = '-c 1' command = ['ping', param, ip] num = 0 while (num < 6): return subprocess.check_output(command) num += 1 run(host='0.0.0.0', port=8080) |
当前输出到浏览器如下:
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8:
icmp_seq=1 ttl=54 time=17.7 ms --- 8.8.8.8 ping statistics --- 1
packets transmitted, 1 received, 0% packet loss, time 0ms rtt
min/avg/max/mdev = 17.756/17.756/17.756/0.000 ms
但它不像我期望的那样打印出5个Ping。它只打印出一个ping,是不是出于某种原因只打印出最后一个ping?如果您从shell命令运行它,我如何打印出5个ping文件。
谢谢。
循环只运行一次,因为
1 2 3 4 5 6 7 8 9 10 | def ping(ip): param = '-c 1' command = ['ping', param, ip] num = 0 output = [] while (num < 6): output.append(subprocess.check_output(command)) num += 1 return output // or return" ".join(output) |