酷代码 AI
菜单
服务商

用python写一个端口扫描程序

```python import socket import threading from queue import Queue target = "127.0.0.1" # 扫描目标IP queue = Queue() open_ports = [] def port_scan(port): try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(1) result = sock.connect_ex((target, port)) if result == 0: open_ports.append(port) sock.close() except Exception: pass def fill_queue(port_list): for port in port_list: queue.put(port) def worker(): while not queue.empty(): port = queue.get() port_scan(port) port_list = range(1, 1024) # 扫描端口范围 fill_queue(port_list) thread_list = [] for _ in range(100): # 线程数量 thread = threading.Thread(target=worker) thread_list.append(thread) for thread in thread_list: thread.start() for thread in thread_list: thread.join() print("开放端口:", open_ports) ``` 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 1. 扫描他人网络或未经授权的系统可能违反法律法规,务必获得明确授权 2. 线程数量设置过高可能导致系统资源耗尽或触发目标防护机制 3. 超时时间过短可能漏报有效端口,过长会显著降低扫描效率 [2025-11-25 14:13:29 | AI写代码神器 | 315点数解答]

相关提问
发财导航,免费问AI
实用工具查看更多