import threading, socket, sys, time
from queue import Queue
print_lock = threading.Lock()
if len(sys.argv) !=2 :
print ("Usage: portscan.py <host>")
sys.exit(1)
host = sys.argv[1]
def scan(port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
con = s.connect((host, port))
with print_lock:
print('Port: ' + str(port) + ' is open')
con.close()
except:
pass
def threader():
while true:
worker = q.qet()
scan(worker)
q.task_done()
q = Queue()
for x in range(100):
t = threading.Thread(target=threader)
t.daemon = True
t.start
for worker in range(1, 1024):
q.put(worker)
q.join()
当我运行它时,它只是挂起,我没有得到任何结果,它不会给我任何错误。我不知道为什么它不起作用。当我不添加线程功能时,它确实有效。
答案 0 :(得分:2)
有三个小错误:
这个更正的代码似乎对我有用:
import threading, socket, sys, time
from queue import Queue
print_lock = threading.Lock()
if len(sys.argv) !=2 :
print ("Usage: portscan.py <host>")
sys.exit(1)
host = sys.argv[1]
def scan(port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
con = s.connect((host, port))
with print_lock:
print('Port: ' + str(port) + ' is open')
con.close()
except:
pass
def threader():
while True:
worker = q.get()
scan(worker)
q.task_done()
q = Queue()
for x in range(100):
t = threading.Thread(target=threader)
t.daemon = True
t.start()
for worker in range(1, 1024):
q.put(worker)
q.join()
答案 1 :(得分:0)
for x in range(100):
t = threading.Thread(target=threader)
t.daemon = True
t.start() # <--- you have to call start? or is this a typo