为什么handle_read方法不被asyncore调用?

发布于 2024-07-16 01:12:43 字数 1233 浏览 9 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神也荒唐 2024-07-23 01:12:43

在同事的帮助下,我终于成功了。 这与将协议参数传递给 create_socket() 方法有关。 不幸的是,调度程序的 create_socket() 不接受第三个参数 - 因此我必须修改我的 packet_socket() 构造函数以采用协议为 < code>ETH_P_ALL (或您希望接收的任何协议类型)作为参数。 编辑以下代码:



#!/usr/bin/python

import asyncore, socket, IN, struct

proto=3
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(3))
s.bind(("eth0",proto))

class packet_socket(asyncore.dispatcher):

    def __init__(self,sock):
        asyncore.dispatcher.__init__(self,sock)
        #self.create_socket(socket.AF_PACKET, socket.SOCK_RAW,socket.htons(3))
        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(s)

asyncore.loop()


谢谢,

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. Unfortunately create_socket() of the dispatcher doesn't take a third argument - so I had to modify my packet_socket() constructor to take a pre-created socket with protocol as ETH_P_ALL (or whatever protocol type you desire to receive) as an argument. Edited code below:



#!/usr/bin/python

import asyncore, socket, IN, struct

proto=3
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(3))
s.bind(("eth0",proto))

class packet_socket(asyncore.dispatcher):

    def __init__(self,sock):
        asyncore.dispatcher.__init__(self,sock)
        #self.create_socket(socket.AF_PACKET, socket.SOCK_RAW,socket.htons(3))
        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(s)

asyncore.loop()


Thanks,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文