撒大大
import ipaddress import re def extract_ips(file_path): with open(file_path, 'r', encoding='utf-8') as file: lines = file.readlines() ipv4_addresses = [] ipv6_addresses = [] for line in lines: # Extract potential IPs using a regular expression potential_ips = re.findall(r'[0-9]+(?:\.[0-9]+){3}|[0-9a-fA-F:]+', line) for ip in potential_ips: try: # Validate and classify the IPs ip_obj = ipaddress.ip_address(ip) if ip_obj.version == 4: ipv4_addresses.append(ip) elif ip_obj.version == 6: ipv6_addresses.append(ip) except ValueError: # Skip invalid IP addresses continue return ipv4_addresses, ipv6_addresses # Path to your file file_path = r"D:\MemoTrace\data\聊天记录\安全事件上报封堵群(xxxxxx@chatroom)\安全事件上报封堵群.txt" # Extract IPs ipv4s, ipv6s = extract_ips(file_path) print("IPv4 Addresses:") for ip in ipv4s: print(ip) print("\nIPv6 Addresses:") for ip in ipv6s: print(ip)
标签:addresses,python,ip,ips,file,print,path,文本 From: https://www.cnblogs.com/ggborn-001/p/18222032