I am trying to proto-type send/recv via a packet socket using the asyncore dispatcher (code below). Although my handle_write method gets called promptly, the handle_read method doesn't seem to get invoked. The loop() does call the readable method every so often, but I am not able to receive anything. I know there are packets received on eth0 because a simple tcpdump shows incoming packets. Am I missing something?
#!/usr/bin/python
import asyncore, socket, IN, struct
class packet_socket(asyncore.dispatcher):
def __init__(self):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_PACKET, socket.SOCK_RAW)
self.buffer = '0180C20034350012545900040060078910'
self.socket.setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,struct.pack("%ds" % (len("eth0")+1,), "eth0"))
def handle_close(self):
self.close()
def handle_connect(self):
pass
def handle_read(self):
print "handle_read() called"
data,addr=self.recvfrom(1024)
print data
print addr
def readable(self):
print "Checking read flag"
return True
def writable(self):
return (len(self.buffer) > 0)
def handle_write(self):
print "Writing buffer data to the socket"
sent = self.sendto(self.buffer,("eth0",0xFFFF))
self.buffer = self.buffer[sent:]
c = packet_socket()
asyncore.loop()
Thanks in advance.