You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Python Socket编程遭遇WinError 10038错误:操作涉及非Socket对象

分析错误原因及解决方案

首先,这个OSError: [WinError 10038] 操作涉及一个非Socket对象错误,核心原因是你在已经关闭Socket连接后,仍然尝试使用这个无效的Socket对象进行recv操作

具体触发场景

看你的代码逻辑:当客户端验证失败时,你执行了client.close(),但while True循环并没有终止,下一次循环依然会调用client.recv(1024)——这时候client已经是被关闭的无效对象,不再是有效的Socket,所以系统抛出了这个错误。

另外,你的消息处理逻辑还有个小问题:msg.decode()执行后没有赋值给变量,后续直接用str(msg)处理bytes对象,会得到类似"b'VERIFICATION:xxx'"的字符串,这会导致你后续的"VERIFICATION:" in msg判断出现偏差。

可行的解决方案

我给你整理了几个关键修复点:

  1. 验证失败后终止循环
    调用client.close()后,立即跳出while循环,避免后续操作无效的Socket对象。

  2. 修正消息解码逻辑
    将bytes对象正确解码为字符串,替换掉错误的str(msg).replace写法。

  3. 优化字符串替换的赋值
    你写的msg.replace("VERIFICATION:", "")没有赋值给变量,这个操作不会改变原字符串(Python字符串是不可变的),需要把结果赋值给新变量才能用。

修正后的完整代码

import socket
import time

unverifyed_clients = []
required_verification = "REQUIRED_VERIFICATION"
required_verification = required_verification.encode()
# 记得定义verification_code,比如:
verification_code = "my_secret_code"

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('127.0.0.1', 65535))
serversocket.listen(0)
(client, address) = serversocket.accept()
unverifyed_clients.append(client)
print("A client has connected")

while True:
    try:
        msg = client.recv(1024)
        # 处理客户端断开连接的情况
        if not msg:
            print("Client disconnected")
            client.close()
            break
        
        # 正确解码bytes为字符串
        msg_str = msg.decode().strip()
        print("1:" + msg_str)

        if "VERIFICATION:" in msg_str and client in unverifyed_clients:
            # 提取验证码,注意赋值结果
            submitted_code = msg_str.replace("VERIFICATION:", "").strip()
            if submitted_code == verification_code:
                print(f"Verification successful: {submitted_code}")
                unverifyed_clients.remove(client)
                print("1a")
                time.sleep(1)
            else:
                print("Invalid verification code, closing connection")
                client.close()
                # 跳出循环,不再操作已关闭的Socket
                break
        elif client in unverifyed_clients and "VERIFICATION:" not in msg_str:
            client.send(required_verification)
            time.sleep(1)
            print(3)
        elif client not in unverifyed_clients and "VERIFICATION:" not in msg_str:
            print("Brain: " + msg_str)
            print(4)
        else:
            print(5)
    except Exception as e:
        print(f"Error occurred: {e}")
        client.close()
        break

额外说明

  • 我添加了try-except块来捕获其他可能的异常,比如客户端意外断开的情况。
  • 新增了if not msg的判断,处理客户端主动断开时recv返回空bytes的场景。
  • 补充了verification_code的定义(你的原代码里没看到这个变量,应该是遗漏了)。

内容的提问来源于stack exchange,提问作者TheMiCraft

火山引擎 最新活动