|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import os
cmd =os.popen("ping 192.168.1.1")
text = cmd.read()
print(text)
普通这个就能正常运作ping
结果如下:
Pinging 192.168.1.1 with 32 bytes of data:
Reply from 192.168.1.1: bytes=32 time=1ms TTL=64
Reply from 192.168.1.1: bytes=32 time=2ms TTL=64
Reply from 192.168.1.1: bytes=32 time=268ms TTL=64
Reply from 192.168.1.1: bytes=32 time=1ms TTL=64
Ping statistics for 192.168.1.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 1ms, Maximum = 268ms, Average = 68ms
我刚刚才学的 Thread
用多线程 Thread 就全都出现 timed out
代码如下:
import os
from threading import Thread
import time
class ping_ip(Thread):
def __init__(self,ip,ip_result):
Thread.__init__(self)
self.ip = ip
self.ip_result = ip_result
def run(self):
time.sleep(1)
cmd =os.popen("ping %s"% self.ip)
text = cmd.read()
print(text)## ---------------------------------------这里打印出来的结果是 :Request timed out.
if "TTL" in text:
self.ip_result.append("OK - ip :" + self.ip)
else:
self.ip_result.append("fail - ip :" + self.ip)
cmd.close()
return self.ip_result
ip_result = []
thread_list = []
num_all = range(1,4)##先实验1到4
for num in num_all:
ip = "196.168.1.%s"%num
t = ping_ip(ip,ip_result)
thread_list.append(t)
t.start()
for t in thread_list:
t.join()
我也试过加入time.sleep ,感觉不关事
网上各种爬文也没有找到解答。
网上有很多现成能用的批量ping代码。
但是我就不懂为什么我这个会出现这个问题。
是os.pepon不能结合Thread 用吗?还是什么问题
望大佬们帮帮
本帖最后由 天圆突破 于 2018-7-17 12:59 编辑
代码没问题,可以正常运行。
是你的ip输错了
|
|