# -*- coding: utf-8 -*- ''' Program:端口扫描器 Function:利用socket库连接端口,进行IP的开放端口扫描 Version:Python3.6 Time:2017/10/13 Author:Walks 个人博客:http://www.bywalks.com ''' from socket import * import threading #建立线程锁 lock = threading.RLock() openNum = 0 threads = [] #端口扫描函数 def portScanner(host,port): global openNum try: s = socket(AF_INET,SOCK_STREAM) s.connect((host,port)) if lock.acquire(): openNum+=1 print('[+] %d open' % port) lock.release() s.close() except: pass def main(): setdefaulttimeout(1) #扫描1-1024端口 for p in range(1,1024): t = threading.Thread(target=portScanner,args=('119.188.97.150',p)) threads.append(t) t.start() for t in threads: t.join() print('[*] The scan is complete!') print('[*] A total of %d open port ' % (openNum)) if __name__ == '__main__': main()