我正在尝试写一个端口扫描器,我需要快速。 我在Google上找到了我尝试使用的线程模块,但是该程序不会更改端口
程序:
from datetime import datetime
from scapy.all import *
import threading
import Queue
def TCPort(ip_addr, port):
time = 1 # max time to send packages to a port is 1 seconds
print port
pkt = IP(dst=ip_addr)/TCP(dport=port,flags="S")
ans = sr1(pkt, timeout=time, verbose=0)
if(str(type(ans))=="<type 'NoneType'>"):
#port is closed
pass
elif ans.haslayer(TCP):
if (ans.getlayer(TCP).flags=="A") or (ans.getlayer(TCP).flags=="SA"):
# port sent a SYN or SYN-ACK answer, so answer with RST to close
# the connection and print the port
sr1(IP(dst=ip_addr)/TCP(dport=port,flags="S"), timeout=time, verbose=0)
print port,"open".rjust(9-len(str(port)))
def multi_thread():
while True:
ip_and_port = q.get()
TCPort(ip_addr, port)
q.task_done()
#main
ip_addr = "43.230.209.5"
ports = range(1,100)
start = datetime.now()
print "Started on", start.strftime('%Y-%m-%d %H:%M:%S')
q = Queue.Queue()
for x in range(30):
t = threading.Thread(target=multi_thread)
t.daemon = True
t.start()
for port in ports:
q.put((ip_addr,port))
q.join()
print "Finished on", datetime.now()-start
当我运行程序时,结果是:
Started on 2019-02-21 11:58:36
24
99
99
99
.
.
Finished on 0:00:33.538000
问题是我在哪里错了?