24 lines
501 B
Python
24 lines
501 B
Python
class SocketReader(object):
|
|
def __init__(self, fp):
|
|
self._fp = fp
|
|
self.handlers = []
|
|
|
|
def add_handler(self, handler):
|
|
self.handlers.append(handler)
|
|
|
|
def read(self, n=-1):
|
|
buf = self._fp.read(n)
|
|
if not buf:
|
|
return ''
|
|
for handler in self.handlers:
|
|
handler(buf)
|
|
return buf
|
|
|
|
def tell(self):
|
|
raise IOError('Stream is not seekable.')
|
|
|
|
|
|
def wrap_with_handler(in_fp, handler):
|
|
wrapper = SocketReader(in_fp)
|
|
wrapper.add_handler(handler)
|
|
return wrapper
|