码迷,mamicode.com
首页 > 编程语言 > 详细

python扫面端口

时间:2018-11-08 12:41:45      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:rom   app   target   decode   def   pop   start   []   代码   

利用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扫面端口

标签:rom   app   target   decode   def   pop   start   []   代码   

原文地址:https://www.cnblogs.com/kmnskd/p/9928357.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!