Python多线程实现网络端口扫描

思路:
1.接收输入,把接收来的网址转化成ip地址【 socket.gethostbyname】
2.建立port空列表
3.设置超时间隔【 socket.setdefaulttimeout  0.5 】
4.scan端口扫描函数
5.建立进程池
6进程池加载scan,传参【.map】
代码:v0.1
import socket
from multiprocessing.dummy import Pool as ThreadPool
from datetime import datetime
input_address = raw_input( 'Please put scan address:' )
ip = socket.gethostbyname(input_address)
port = []
for i in range( 1 , 28017 ):
    port.append(i)
socket.setdefaulttimeout( 0.5 )
def scan (port):
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    try :
        fal = s.connect_ex((ip,port))
        res = s.recv( 1024 )
        if fal == 0 :   
            print 'port : ' +str(port) + ' is open' + res
            s.close()
    except Exception, e:
        print str(e.message)
t1 = datetime.now()
pool = ThreadPool(processes = 20 )
result = pool.map(scan,port)
pool.close()
pool.join()
print 'using time : ' , datetime.now() - t1    

猜你喜欢

转载自blog.csdn.net/bing767573382/article/details/80643882