我正在处理此代码
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()
我收到此错误:
OSError: [WinError 10038] an operation was attempted on something that is not a socket
答案 0 :(得分:12)
在仅读取部分数据后,您正在关闭clientsock。
clientsock.close()
处于错误的缩进级别。将其向左移动一步。
答案 1 :(得分:1)
问题在于 close()。 连接关闭后需要重新初始化连接
在while循环中加入这一行
serversock = socket(AF_INET, SOCK_STREAM)