0

我想在一行中手动写出 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
4

1 回答 1

2

您的问题可能与 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()
于 2013-02-08T23:37:24.967 回答