【发布时间】:2013-07-05 00:37:25
【问题描述】:
我正在尝试以最快的方式打印连接到我的网络的所有 Live IP。 我尝试在 for 循环中 ping,但速度很慢:
def PingTry(host):
ping = subprocess.Popen(["ping", host], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
out, error = ping.communicate()
print out #This will show me the ping result, I can check the content and see if the host replyed or not
正如我所说,它非常慢(我需要这样做 255 次)。
我尝试使用带有端口 80 的 TCP 连接来连接它:
import socket
IP = '192.168.1.100'
PORT = 80
tcpsoc = socket(AF_INET, SOCK_STREAM)
tcpsoc.listen(SOMAXCONN)
try:
tcpsoc.bind(ADDR)
except Exception,ex:
print "host is down!"
但是,它仍然不适用于此 IP,尽管它适用于路由器 IP
有没有办法更快地获取所有实时 IP?
【问题讨论】:
-
ping 慢的原因是您实际上是在等待来自界面的响应(pong)。您可以改为使用 arp -a 生成已连接接口的列表。
-
我尝试使用arp -a,但我只在接口列表中获取了路由器的IP地址,虽然我的手机和我的笔记本电脑也已连接到网络
-
您使用的是什么操作系统?你能确定你有超级用户(或至少
CAP_NET_ADMIN)权限吗? -
@phihag 我在 Windows 7 64 位上,是的,我有管理员权限。
标签: python windows networking ip lan