【问题标题】:Data send disallowed? [WinError 10057]不允许发送数据? [Win 错误 10057]
【发布时间】: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 识别为坏人。这两条信息都可能对攻击者有用。不要告诉他们什么。

标签: python sockets tcp server


【解决方案1】:

我想通了!根据其他人的[WinError 10057] 问题,服务器拒绝使用某个 sock 变量。已经过了 6 小时,我决定检查我的客户端代码,结果发现我首先在 __init__ 函数中将 client 变量声明为普通套接字。然后在我的connect 函数中获取此变量并将其修改为连接变量(因此 send 和 recv 函数在该函数中工作)。然后我的发送函数重新读取原始套接字(非连接)并使用它发送(失败)。所以我做了非常复杂的等效版本(用服务器的话)做conn, addr = s.accept() 然后做old_s = s 然后s = conn 然后愚蠢地重置 conn = old_s。这是我能解释的最好的了,如果你没有听懂的话,我很抱歉。

【讨论】:

    猜你喜欢
    • 2021-10-11
    • 2014-04-20
    • 1970-01-01
    • 2020-04-09
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多