I'm trying to just make a simple asyncore example where one socket is the sender and one is the receiver. For some reason, the handle_read() on the receiver is never called so I never get the 'test' data. Anyone know why? This is my first shot at asyncore, so it's probably something extremely simple.
import asyncore, socket, pdb, random
class Sender(asyncore.dispatcher):
def __init__(self):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
def handle_connect(self):
print ('first connect')
def writable(self):
True
def readable(self):
return False
def handle_write(self):
pass
def handle_close(self):
self.close()
class Receiver(asyncore.dispatcher):
def __init__(self):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
def handle_connect(self):
print ('first connect')
def readable(self):
return True
def handle_read(self):
print 'reading'
def handle_write(self):
print 'write'
def handle_accept(self):
self.conn_sock, addr = self.accept()
print 'accepted'
def handle_close(self):
self.close()
a = Sender()
b = Receiver()
addr = ('localhost', 12344)
b.bind(addr)
b.listen(1)
a.connect(addr)
asyncore.loop()
a.send('test')