Python扫描网络上的所有主机

时间:2013-11-03 10:41:29

标签: python

我需要扫描网络上的每个主机,看看他们是否使用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个潜在主机下载页面几乎不会有效。

由于

1 个答案:

答案 0 :(得分:3)

查看nmap

的python绑定