asyncores的handle_close持续不断触发

时间:2015-08-19 19:52:30

标签: python sockets asyncore

self.handlers.append(ConnHandler(sock,self.handlers))我是python的新手,我尝试编写一个简单的套接字服务器来测试内容并更好地了解语言。

import asyncore
import socket
import json

class ConnHandler(asyncore.dispatcher_with_send):

    def __init__(self, conn, handlerlist):
        asyncore.dispatcher_with_send.__init__(self, conn)
        self.handlers = handlerlist

    def handle_close(self):
        self.close()
        print 'Socket closed'
        if(self.handlers.count(self) > 0):
            self.handlers.remove(self);

    def handle_read(self):
        data = ''
        more = True
        while more:
            try:
                data += self.recv(1024)
            except socket.error, e:
                more = False

        if data == '':
            return

        try:
            message = json.loads(data)
        except ValueError:
            self.send('Invalid JSON\n')
            return

        print message


class TestServer(asyncore.dispatcher):

    def __init__(self, host, port):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind((host, port))
        self.listen(5)
        self.handlers = []

    def handle_accept(self):
        pair = self.accept()
        if pair is not None:
            sock, addr = pair
            print 'Incoming connection from %s' % repr(addr)
            self.handlers.append(ConnHandler(sock, self.handlers))

    def sendToAll(self, string):
        for h in self.handlers:
            h.send(string + '\n')

server = TestServer('localhost', 55555)
asyncore.loop()

我的问题如下。 如果我用telnet连接到服务器,然后退出telnet关闭连接,python只是spams' Socket关闭'在终端。我怎样才能解决这个问题?上面的代码中是否有明显的初学者错误?

1 个答案:

答案 0 :(得分:0)

handle_close无意检测对等方是否已断开连接,如果handle_read调用返回空字符串,则会在.recv中获取此信息。然后你可以关闭套接字,然后调用handle_close

以下是您的代码的修改版本:

def handle_close(self):
    print 'Socket closed'
    if(self.handlers.count(self) > 0):
        self.handlers.remove(self);

def handle_read(self):
    data = ''
    more = True
    while more:
        try:
            new_data = self.recv(1024)
            if not new_data:
                self.close()
                return
            else:
                data += new_data
        except socket.error, e:
            more = False

    if data == '':
        return

    try:
        message = json.loads(data)
    except ValueError:
        self.send('Invalid JSON\n')
        return

    print message