Created
March 9, 2020 09:24
-
-
Save tonybaloney/07af251dea2e0062deba0e8e77c9b6ac to your computer and use it in GitHub Desktop.
TCP Port 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
from queue import Queue | |
import socket | |
import time | |
timeout = 1.0 | |
def check_port(host: str, port: int, results: 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() | |
host = "something-you-own.com" | |
results = Queue() | |
scan_range = (1, 1000) | |
for port in range(*scan_range): | |
check_port(host, port, results) | |
while not results.empty(): | |
print("Port {0} is open".format(results.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