Created
March 9, 2020 09:30
-
-
Save tonybaloney/b85fb33b2f674994d87fed283c8f125b to your computer and use it in GitHub Desktop.
multiprocessing TCP scanner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing as mp | |
import time | |
import socket | |
timeout = 1 | |
def check_port(host: str, port: int, results: mp.Queue): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.settimeout(timeout) | |
result = sock.connect_ex((host, port)) | |
if result == 0: | |
results.put(port) | |
sock.close() | |
if __name__ == '__main__': | |
start = time.time() | |
processes = [] | |
scan_range = range(80, 100) | |
host = "something-you-own.com" | |
mp.set_start_method('spawn') | |
pool_manager = mp.Manager() | |
with mp.Pool(len(scan_range)) as pool: | |
outputs = pool_manager.Queue() | |
for port in scan_range: | |
processes.append(pool.apply_async(check_port, (host, port, outputs))) | |
for process in processes: | |
process.get() | |
while not outputs.empty(): | |
print("Port {0} is open".format(outputs.get())) | |
print("Completed scan in {0} seconds".format(time.time() - start)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment