我有以下代码:
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失败。
但是,根据handle_write,self.send()只应该返回一个整数。而且套接字没有关闭,因为断言失败后调用了handle_close。
客户端接收缓冲区中的数据。
知道我做错了什么/不明白吗?
发布于 2015-01-21 14:30:13
class Handler(asyncore.dispatcher_with_send):
...
sent = self.send(self._out_buffer)
assert sent is not Noneasyncore.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失败了。
示例使用asyncore.dispatcher,其send方法返回发送的字节数。
https://stackoverflow.com/questions/28069183
复制相似问题