我想在一行中手动写出itertools所做的事情,这样我就可以尝试使用多个字符串来更快地完成函数。现在这个函数起作用了,我只需要大大提高它的速度。我甚至不确定写出itertools行是否能让我使用多个线程。
def list ():
charLength = sys.argv[2]
charSet = 'abcdefghijklmnopqrstuvwxyz0123456789'
combo = itertools.combinations_with_replacement(charSet, int(charLength))
for floatingcombo in combo:
floatingcombo = ''.join(floatingcombo)
floatingcombo += "." + sys.argv[3]
try:
floatingIP = socket.gethostbyname(floatingcombo)
msvcrt.printf("%s resolved to --> %s\n", floatingcombo, floatingIP)
except socket.gaierror:
msvcrt.printf("%s does not exist\n", floatingcombo)
return发布于 2013-02-09 07:37:25
您的问题可能与IO有关。您可以通过使用多线程/进程来加速查找。为了避免dns实施中的线程问题,您可以使用多个进程来解析主机:
#!/usr/bin/env python
import itertools
import socket
import sys
from functools import partial
from multiprocessing import Pool
def resolve(host, domain=""):
host = ''.join(host) + domain
try:
return host, socket.gethostbyname(host), None
except EnvironmentError as e:
return host, None, e
def main():
alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789'
size = int(sys.argv[2])
resolve_host = partial(resolve, domain="." + sys.argv[3])
combo = itertools.combinations_with_replacement(alphabet, size)
pool = Pool(20)
for host, ip, error in pool.imap_unordered(resolve_host, combo):
if error is None:
print("%s resolved to --> %s" % (host, ip))
else: # error
print("Can't resolve %s, error: %s" % (host, error))
if __name__=="__main__":
main()https://stackoverflow.com/questions/14782831
复制相似问题