Skip to content

Instantly share code, notes, and snippets.

@Tethik
Created May 2, 2014 12:54
Show Gist options
  • Save Tethik/a58e4df695b853075baf to your computer and use it in GitHub Desktop.
Save Tethik/a58e4df695b853075baf to your computer and use it in GitHub Desktop.
Basic tcp portscan script.
import socket
import threading
from queue import Queue
def is_open(host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(0.5)
try:
s.connect((host, port))
s.close()
return True
except socket.error:
return False
num_worker_threads = 100
mtx = threading.Lock()
def scan(host):
q = Queue()
for p in range(1, 25000):
q.put(p)
def work():
while not q.empty():
p = q.get()
if is_open(host, p):
mtx.acquire()
print(p, "open")
mtx.release()
q.task_done()
for i in range(num_worker_threads):
t = threading.Thread(target=work)
t.daemon = True
t.start()
q.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment