39 lines
887 B
Python
39 lines
887 B
Python
import asyncore, socket
|
|
import logging
|
|
|
|
class IOChannel(asyncore.dispatcher):
|
|
def __init__(self, host, port, callback):
|
|
asyncore.dispatcher.__init__(self)
|
|
self.logger = logging.getLogger(self.__class__.__name__)
|
|
|
|
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
self.connect((host, port))
|
|
|
|
self.callback = callback
|
|
self.buffer = ""
|
|
|
|
def sendData(self, data):
|
|
self.buffer += data
|
|
self.logger.debug("Added Data to buffer. len(buffer) == %d", len(self.buffer))
|
|
|
|
def handle_connect(self):
|
|
pass
|
|
|
|
def handle_close(self):
|
|
self.close()
|
|
|
|
def handle_read(self):
|
|
data = self.recv(65536)
|
|
self.callback(data)
|
|
|
|
def handle_write(self):
|
|
sent = self.send(self.buffer)
|
|
self.buffer = self.buffer[sent:]
|
|
self.logger.debug("Flush buffer. len(buffer) == %d", len(self.buffer))
|
|
|
|
def writable(self):
|
|
return (len(self.buffer) > 0)
|
|
|
|
def readable(self):
|
|
return True
|