Reputation: 3
I would like to manually write out what itertools does in one line so that I can attempt to use multiple strings to work through the function quicker. Right now this function works, I just need to massively speed it up. I am not really even sure if writing out the itertools line would enable me to use multiple threads.
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
Upvotes: 0
Views: 382
Reputation: 414695
Your problem is probably IO-bound. You could speed up the lookup by using multiple threads/processes. To avoid threading issues in dns implementation, you could use multiple processes to resolve hosts:
#!/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()
Upvotes: 2