transpub/Spectrum2/iochannel.py

36 lines
674 B
Python
Raw Normal View History

2013-05-31 18:53:21 +00:00
import asyncore, socket
2015-02-27 16:06:30 +00:00
import logging
2013-05-31 18:53:21 +00:00
class IOChannel(asyncore.dispatcher):
def __init__(self, host, port, callback):
asyncore.dispatcher.__init__(self)
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
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:]
def writable(self):
return (len(self.buffer) > 0)
def readable(self):
return True