asyncore.dispatcher_with_send.send() returns None

asyncore.dispatcher_with_send.send() returns None

我有以下代码:

class Handler(asyncore.dispatcher_with_send):
    def __init__(self, class_, sock):
        super().__init__(sock)
        # ...

    # ...

    def writable(self):
        return self.generator or self._out_buffer
    def handle_write(self):
        # ...

        sent = self.send(self._out_buffer)
        import sys
        print(self._out_buffer)
        sys.stdout.flush()
        assert sent is not None
        # ...
        self._out_buffer = self._out_buffer[sent:]
        if not self._out_buffer:
            print('Closing connection.')
            self.close()

    def handle_close(self):
        print('connection closed.')
        super().handle_close()

谁的输出是:

Incoming connection from ('127.0.0.1', 39045)
b'400 Bad Request\r\n\r\n'
error: uncaptured python exception, closing channel <ppp_libmodule.async_http.Handler connected 127.0.0.1:39045 at 0x7f27c9ab6d30> (<class 'AssertionError'>: [/usr/lib/python3.4/asyncore.py|write|91] [/usr/lib/python3.4/asyncore.py|handle_write_event|461] [/home/progval/.local/lib/python3.4/site-packages/ppp_libmodule-0.7.2-py3.4.egg/ppp_libmodule/async_http.py|handle_write|71])
connection closed.

如您所见,断言 sent is not None 失败。

但是,根据 the example given for handle_writeself.send() 应该只是 return 一个整数。 并且套接字还没有关闭,因为断言失败后调用了handle_close

并且客户端接收缓冲区中的数据。

知道我做错了什么/没理解吗?

class Handler(asyncore.dispatcher_with_send):
    ...
    sent = self.send(self._out_buffer)
    assert sent is not None

asyncore.dispatcher_with_send:

class dispatcher_with_send(dispatcher):
    ...    
    def send(self, data):
        if self.debug:
            self.log_info('sending %s' % repr(data))
        self.out_buffer = self.out_buffer + data
        self.initiate_send()

dispatcher_with_send.send returns 什么都没有,所以你的 assert 失败了。

example 使用 asyncore.dispatcher,其 send 方法 returns 发送的字节数。