1. 程式人生 > >python掃面端口

python掃面端口

spa print append pen host () sock tcp連接 target

利用TCP連接創建一個socket掃描port

上代碼:

#TCP
import socket
from threading import Thread
def get_port(open_port,host,ports):
    while True:
        if ports:
            port = ports.pop(0)
            c = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
            c.settimeout(0.5)
            try:
                c.connect((host,port))
            
except socket.timeout as e: print([-]%d % port) del c else: print([+]%d % port) c.send(1.encode()) portocol = c.recv(1024).decode() open_port[port] = portocol c.close()
del c else: break def main(): host = "192.168.1.33" open_port = {} ports = list(range(1,65535)) t_list = [] for var in range(100): t_list.append(Thread(target=get_port,args=(open_port,host,ports,))) for t in t_list: t.start() for t in t_list: t.join() get_port(open_port,host,ports)
print(open_port) if __name__ == __main__: main()

python掃面端口