目前,我有一个客户端和一个服务器程序。首先,我希望客户端扫描并寻找网络上开放的端口,我将最终使用这些端口,以便客户端只要在本地网络上获得一个端口即可找到正确的服务器。之后,我想向服务器发送消息。扫描包括服务器的本地网络后,无法从客户端发送消息,出现错误“连接被拒绝”。如何扫描,然后将消息发送到服务器而没有任何一方锁定?客户端的运行速度也很慢,大概是因为有很多扫描要做。有什么方法可以使其更高效?
IP地址是匿名的,并不意味着在所有接口上监听。
我正在使用nmap。我主要尝试从客户端进行操作。我已经修改了scan()内部的参数,但没有用。我的服务器和客户端代码在下面。
#Server
#!/usr/bin/env python3
import socket
HOST = '00.0.0.00'
PORT = 2108
# create TCP/IP socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# bind the socket to an IP/host and port
s.bind((HOST, PORT))
# listen for connections, default queue length
s.listen(10)
print(f'Listening at {HOST} on port {PORT}')
# accept outside connections (blocking)
client_socket, (client_address, client_port) = s.accept()
with client_socket:
print(f'Accepted new connection from {client_address}:{client_port}')
while True:
data = client_socket.recv(1024)
if not data:
print(f'Client {client_address} closed connection')
break
print(f'Received from client: {data}')
client_socket.sendall(data)
# Client
import socket
import nmap
HOST = '00.0.0.00'
PORT = 2108
nm = nmap.PortScanner()
print (nm.nmap_version())
nm.scan('00.0.0.00/24', '1-3000', '-v')
print(nm.scaninfo())
print(nm.csv())
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
print(f'Sending to server: {DATA}')
s.sendall(DATA)
data = s.recv(1024)
print(f'Received from server: {data}')
从服务器端运行代码,我得到了错误
ConnectionResetError: [Errno 104] Connection reset by peer
从客户端,我得到:
ConnectionRefusedError: [Errno 111] Connection refused
错误来自哪里?客户端还是服务器端?还是两者兼而有之?错误似乎矛盾。