Skip to content

Commit

Permalink
Fixing up threading for downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
stavinski committed Dec 3, 2022
1 parent 05fb50f commit 3abb9b8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions download.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class Downloader(object):

def __init__(self, api, thread_count, ips, delay=1):
def __init__(self, api, thread_count, ips, delay=1, processed_callback=lambda _: None):
self.lock = Lock()
self.api = api
self.thread_count = thread_count
Expand All @@ -14,13 +14,15 @@ def __init__(self, api, thread_count, ips, delay=1):
self.queue = Queue(len(ips))
self.threads = []
self.results = []
self._start_threads(processed_callback)

def download(self, processed_callback=lambda _: None):
def _start_threads(self,processed_callback):
for count in range(0, self.thread_count):
thread = Thread(target=self._download_host, name=f'downloader-{count}', args=(processed_callback,))
thread.setDaemon(True)
thread.start()

def download(self):
for ip in self.ips:
self.queue.put(ip)
sleep(self.delay) # shodan API is rate limited so requires a delay between reqs
Expand Down
4 changes: 2 additions & 2 deletions showdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ def main(args):
log.write(colored('[-] No details', 'red'))
log.write(f"[+] Processing {len(ips)} hosts: ", end='', flush=True)

downloader = Downloader(api, args.threads, ips)
hosts = downloader.download(processed_callback=ip_processed)
downloader = Downloader(api, args.threads, ips, processed_callback=ip_processed)
hosts = downloader.download()
print()

# no details for any of the hosts, nothing more to do!
Expand Down

0 comments on commit 3abb9b8

Please sign in to comment.