我需要掃描網絡上的每臺主機,以查看它們是否使用python在端口80上託管web服務器。我寫了一個腳本來做到這一點,但它的效率令人難以置信。是否有可以更有效地做這個任務Python掃描網絡上的所有主機
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潛在主機下載頁面難言有效的更好的辦法任何模塊或庫。
感謝
這個問題Python綁定可能適合http://codereview.stackexchange.com – aIKid