Skip to content

Instantly share code, notes, and snippets.

@tonybaloney
Created March 9, 2020 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonybaloney/07af251dea2e0062deba0e8e77c9b6ac to your computer and use it in GitHub Desktop.
Save tonybaloney/07af251dea2e0062deba0e8e77c9b6ac to your computer and use it in GitHub Desktop.
TCP Port scanner
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