我需要扫描网络上的每台主机,看看它们是否使用 python 在端口 80 上托管了一个网络服务器。我写了一个脚本来做到这一点,但它的效率低得令人难以置信。是否有任何模块或库可以更有效地完成此任务
def scanhosts():
for ifaceName in interfaces():
addresses = [i['addr'] for i in ifaddresses(ifaceName).setdefault(AF_INET, [{'addr':'No IP addr'}] )]
if ifaceName == "wlan0":
return ', '.join(addresses)
def scanlooper(subnet):
for i in range(0,1):
for x in range(0,999):
s = "%s.%s.%s" % (subnet,i,x)
request = urllib2.Request('http://%s/' % s)
try:
response = urllib2.urlopen(request)
html = response.read()
if html != "False":
print "%s is hosting a server" % s
except:
print "%s is not hosting a server" % s
localip = scanhosts()
ipstrip = localip.strip(".")
subnet = "%s.%s" % (ipstrip[0],ipstrip[1])
scanlooper(subnet)
显然有一个更好的方法来执行这个任务,从 1'000'000 个潜在主机下载一个页面几乎没有效率。
谢谢