python3多线程端口扫描
#!/usr/bin/env python
import socket
import threading
import queue
def get_ip_status(ip, port):
server = socket.socket()
try:
server.settimeout(1)
server.connect((ip, port))
print('{0} port {1} is open'.format(ip, port))
with open('./success.txt', 'a+') as f:
f.write('{0} port {1} is open\n'.format(ip, port))
except Exception as err:
print(err)
finally:
server.close()
def check_open(q):
try:
while not q.empty():
ip = q.get()
for port in range(20, 90):
get_ip_status(ip, port)
except:
pass
if __name__ == '__main__':
#lock = threading.Lock()
q = queue.Queue()
with open("ip.txt", 'r') as f:
hosts = f.readlines()
for host in hosts:
q.put(host.strip())
threads = []
n_thread = 50
for i in range(n_thread):
t = threading.Thread(target=check_open, args=(q,))
t.start()
threads.append(t)
for t in threads:
t.join()
网友评论