【发布时间】:2020-10-11 01:18:21
【问题描述】:
我知道这是在应该使用 conn 变量时使用 s 变量的服务器部分的错误,但我已经在这里坐了 2 个小时,看不到错误。错误:[WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
我的代码:
import socket
from _thread import *
server = '123.456.78.9'
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((server, port))
except socket.error as e:
str(e)
s.listen()
print("Waiting for connections, server has been started")
def threaded_client(conn):
reply = ""
conn.sendall(str.encode("[Server, Server]Mis:200:Connected"))
while True:
try:
data = conn.recv(2048)
reply = data.decode("utf-8")
if not data:
print("Disconnected from", addr[0])
break
print("Received: ", reply)
print("Sending: ", reply)
conn.sendall(str.encode(reply))
except:
break
print("Connection to", addr[0], "has been lost!")
conn.close()
while True:
conn, addr = s.accept()
banlist = open('bannedip.bipf')
if addr[0] in banlist.read():
conn.sendall(str.encode("[Server, Server]Err:401:Banned"))
conn.close()
print("Banned ip", addr[0], ", was disconnected as their ip (", addr[0], ") is listed in the ban file")
else:
print("Connected to:", addr[0])
start_new_thread(threaded_client, (conn,))```
【问题讨论】:
-
你是对的。你确定这是真正的代码吗?注意我建议您不要向被禁止的 IP 发送任何内容。根本不要向他们泄露任何信息。只需关闭连接即可。
-
请问这会泄漏什么?只是好奇
-
另外,是的,这是我的代码,直接复制和粘贴,除了 ip
-
它会泄露以下信息:(a) IP:port 存在,(b) 您将他们的 IP:port 识别为坏人。这两条信息都可能对攻击者有用。不要告诉他们什么。