美文网首页
python3多线程端口扫描

python3多线程端口扫描

作者: migrate_ | 来源:发表于2021-01-12 10:53 被阅读0次

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()

相关文章

网友评论

      本文标题:python3多线程端口扫描

      本文链接:https://www.haomeiwen.com/subject/bpusaktx.html