python实现的端口扫描程序(多线程版)

python实现的端口扫描程序,该程序的弊端是由于对线程数量没有做限制,所以会导致大量内存被占用,但是也有好处,就是速度会比较快。
【声明:该工具仅用于学术研究用,请勿用作其他用途,若产生不良结果,由使用者自己承担责任】


#!/usr/bin/env python
import threading
import socket
import time

class myThread (threading.Thread):
    def __init__(self, threadID, name, ip, port):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.ip = ip
        self.port = port

    def run(self):
        get_ip_status(self.ip, self.port)


def get_ip_status(ip,port):
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        server.connect((ip,port))
        print('{0} port {1} is open'.format(ip, port))
    except Exception as err:
         print('{0} port {1} is not open'.format(ip,port))
        pass
    finally:
        server.close()
 
if __name__ == '__main__':
    # host = '47.52.227.218'
    host = '127.0.0.1'
    threadList1 = []
    t1 = time.time()
    for port in range(1, 65536):
        # 创建新线程
        threadList1.append(myThread(port, "Thread-" + str(port), host, port))
    
    for item in threadList1:
        item.start()
    
    for item in threadList1:
        item.join()

    print(time.time() - t1)
    print("done")
    

你可能感兴趣的:(python,系统安全)