python套接字编程OSError:[WinError 10038]尝试对不是套接字的东西进行操作

use*_*251 7 python sockets

我正在研究这段代码

from socket import *
HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.bind(ADDR)
serversock.listen(2)

while 1:
    print ("waiting on connection")
    clientsock, addr = serversock.accept()
    print ('connected from:', addr)
    while 1:
        data = clientsock.recv(1024).decode()
        if not data: break 
        clientsock.send(data.encode())
        clientsock.close()

serversock.close()
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

OSError: [WinError 10038] an operation was attempted on something that is not a socket
Run Code Online (Sandbox Code Playgroud)

Rob*_*obᵩ 13

在仅读取部分数据后,您将关闭clientsock.

clientsock.close()
Run Code Online (Sandbox Code Playgroud)

是在错误的缩进水平.将其向左移动一步.