asyncore.dispatcher_with_send.send()返回None

时间:2015-01-21 14:06:19

标签: python python-3.x asyncore

我有以下代码:

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()应该只返回一个整数。 并且套接字尚未关闭,因为在断言失败后调用handle_close

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

知道我做错了什么/不明白?

1 个答案:

答案 0 :(得分:2)

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不返回任何内容,因此assert失败。

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