python实现端口全扫描程序设计
时间: 2024-10-08 14:09:44 浏览: 24
python nmap实现端口扫描器教程
在Python中,编写端口扫描程序通常是为了测试网络服务的可用性和安全性。以下是一个简单的TCP端口扫描程序的设计示例,使用socket模块:
```python
import socket
def scan_port(ip, port_range):
for port in range(port_range[0], port_range[1]+1):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5) # 设置超时时间,防止阻塞
try:
result = sock.connect_ex((ip, port))
if result == 0:
print(f"Port {port} is open on {ip}")
else:
print(f"Port {port} is closed or not responding on {ip}")
except (socket.timeout, ConnectionRefusedError):
pass # 网络不可达或超时处理
finally:
sock.close()
# 使用示例,扫描80到1000之间的端口
scan_port('192.168.1.1', (80, 1000))
阅读全文