为什么handle_read方法不被asyncore调用?
我正在尝试使用 asyncore 调度程序通过数据包套接字进行原型发送/接收(代码如下)。 虽然我的handle_write方法被立即调用,但handle_read方法似乎没有被调用。 Loop() 确实经常调用可读方法,但我无法接收任何内容。 我知道 eth0 上收到了数据包,因为一个简单的 tcpdump 显示了传入的数据包。 我错过了什么吗?
#!/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()
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在同事的帮助下,我终于成功了。 这与将协议参数传递给 create_socket() 方法有关。 不幸的是,调度程序的
create_socket()
不接受第三个参数 - 因此我必须修改我的packet_socket()
构造函数以采用协议为 < code>ETH_P_ALL (或您希望接收的任何协议类型)作为参数。 编辑以下代码:谢谢,
I finally got this to work with some help from a co-worker. This has to do with passing the protocol argument to the
create_socket()
method. Unfortunatelycreate_socket()
of the dispatcher doesn't take a third argument - so I had to modify mypacket_socket()
constructor to take a pre-created socket with protocol asETH_P_ALL
(or whatever protocol type you desire to receive) as an argument. Edited code below:Thanks,