python多线程扫描端口(线程池)
(编辑:jimmy 日期: 2024/11/21 浏览:3 次 )
扫描服务器ip开放端口,用线程池ThreadPoolExecutor,i7的cpu可以开到600个左右现成,大概20s左右扫描完65535个端口,根据电脑配置适当降低线程数
#!/usr/local/python3.6.3/bin/python3.6 # coding = utf-8 import socket import datetime import re from concurrent.futures import ThreadPoolExecutor, wait DEBUG = False # 判断ip地址输入是否符合规范 def check_ip(ipAddr): compile_ip = re.compile('^(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)$') if compile_ip.match(ipAddr): return True else: return False # 扫描端口程序 def portscan(ip, port): try: s = socket.socket() s.settimeout(0.2) s.connect((ip, port)) openstr = f'[+] {ip} port:{port} open' print(openstr) except Exception as e: if DEBUG is True: print(ip + str(port) + str(e)) else: return f'[+] {ip} port:{port} error' finally: s.close #主程序,利用ThreadPoolExecutor创建600个线程同时扫描端口 def main(): while True: ip = input("请输入ip地址:") if check_ip(ip): start_time = datetime.datetime.now() executor = ThreadPoolExecutor(max_workers=600) t = [executor.submit(portscan, ip, n) for n in range(1, 65536)] if wait(t, return_when='ALL_COMPLETED'): end_time = datetime.datetime.now() print("扫描完成,用时:", (end_time - start_time).seconds) break if __name__ == '__main__': main()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇:Python数据分析模块pandas用法详解