- 论坛徽章:
- 0
|
本帖最后由 coolxl2014 于 2014-12-26 16:00 编辑
麻烦大牛帮忙看下这个脚本,用socket.connect分别去连接192.168.2.x这个ip段的15个端口,扫描开放状态。超时是2秒,根据我新手的理解。如果扫描1个ip的15个端口,15个线程,应该2秒内能打印出所以结果。但实际情况是结果一条条往外冒,和单线程一样慢。 求大牛帮小弟指出错误。多谢~- # -*- coding: utf-8 -*-
- import threading, socket
- def host_list(start,end):
- for i in range(start,end,1):
- ip = net+str(i)
- for x in PortList:
- port = x
- tmp = []
- tmp.append(ip)
- tmp.append(port)
- list.append(tmp)
- return list
- def scan(list):
- while True:
- if len(list)>0:
- lock.acquire()
- host = list.pop()
- lock.release()
- ip = host[0]
- port = host[1]
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.settimeout(Timeout)
- address = (ip, port)
- try:
- sock.connect(address)
- print ip,port,'True'
- except:
- sock.close()
- print ip,port,'False'
- sock.close()
- else:
- break
- def threads(x):
- thread_tmp=[]
- for i in range(x):
- new_thread=threading.Thread(target=scan(host), args=(i),name='thread'+str(i))
- thread_tmp.append(new_thread)
- for i in thread_tmp:
- i.start()
- i.join()
- if __name__ == '__main__':
- net = '192.168.2.'
- Timeout = 2.0
- PortList = [21, 22, 23, 25, 80, 135, 137, 139, 445, 1433, 1502, 3306, 3389, 8080, 9015]
- list = []
- true_list = []
- false_list = []
- lock = threading.Lock()
- host = host_list(1,2)
- threads(15)
复制代码 |
|