import socket
from concurrent.futures import ThreadPoolExecutor
# 扫描指定 IP 地址的端口
def scan_port(ip, port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(0.5) # 设置超时时间
result = s.connect_ex((ip, port)) # 尝试连接
if result == 0:
print(f'Port {port} is open')
# else:
# print(f'Port {port} is closed')
def scan_ports(ip):
with ThreadPoolExecutor(max_workers=100) as executor:
# 使用并发扫描
for port in range(1, 65536):
executor.submit(scan_port, ip, port)
if __name__ == '__main__':
target_ip = input("Enter the IP address to scan: ")
print(f"Scanning {target_ip} for open ports...")
scan_ports(target_ip)