尝试关闭套接字时出现OSError

时间:2017-11-08 08:13:31

标签: python python-3.x

我正在尝试运行一个非常简单的服务器来接收连接,测试以查看数据是否为“HelloClient”,如果它是发回“HelloServer”,如果它不是它关闭连接。但是,当它得到任何响应时会抛出错误

OSError: [WinError 10038] An operation was attempted on something that is not a socket

搜索错误并未产生任何结果,导致此问题的原因以及如何解决?

代码参考:

class ClientHandler(socketserver.BaseRequestHandler):
    state = 0

    def handle(self):
        print(self.client_address)
        while True:
            data = self.request.recv(1024)
            if not data: break
            print("Received: ", data, " from ", self.client_address)    # MARKER
            self.parse(data)
        self.request.close()

    def parse(self, data):
        if self.state == 0:
            if data == b"HelloClient":
                self.state = 1
                self.request.send(b"HelloServer")
            else:
                self.request.close()  #PROBLEM LINE

host = ''  
port = 50007  
addr = (self.myHost, self.myPort)
socketserver.ThreadingTCPServer(addr, ClientHandler).serve_forever()

0 个答案:

没有答案