可以帮我用python写一个网络扫描器吗
时间: 2023-06-03 11:02:01 浏览: 58
当然可以!网络扫描器可以使用Python中的socket库来实现。您可以编写一个程序,通过检查终端输入的IP地址和端口范围,从而扫描网络上的活动主机和可用端口。以下是一个示例代码:
```
import socket
target_host = input("Enter the IP address you want to scan: ")
target_port_range = input("Enter the range of ports you want to scan (e.g. 1-100): ")
# Parse the port range
start_port, end_port = target_port_range.split("-")
# Create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Iterate through each port in the range
for port in range(int(start_port), int(end_port)+1):
# Connect to the target host and port
client.connect((target_host, port))
# Print success message if connection is successful
print(f"Port {port} is open")
except:
print("Error: Unable to connect to target host and port")
# Close the socket after all ports have been scanned
client.close()
```
此代码将获取您输入的IP地址和端口范围,并扫描IP地址上的每个端口,打印出哪些端口是打开的。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)