2015-12-02 91 views

回答

0

经过长期搜索,答案是没有。 Asyncore假定底层套接字是面向连接的,即TCP。

2

是的,你可以。这里有一个简单的例子:

class AsyncoreSocketUDP(asyncore.dispatcher): 

    def __init__(self, port=0): 
    asyncore.dispatcher.__init__(self) 
    self.create_socket(socket.AF_INET, socket.SOCK_DGRAM) 
    self.bind(('', port)) 

    # This is called every time there is something to read 
    def handle_read(self): 
    data, addr = self.recvfrom(2048) 
    # ... do something here, eg self.sendto(data, (addr, port)) 

    def writable(self): 
    return False # don't want write notifies 

这应该足以让你开始。看看asyncore模块的更多想法。

小注:asyncore.dispatcher将套接字设置为非阻塞。如果 想要将大量数据快速写入套接字而不会导致 错误,则必须执行一些与应用程序相关的缓冲操作 ala asyncore.dispatcher_with_send

感谢这里让我的(有些不太准确)的代码开始: https://www.panda3d.org/forums/viewtopic.php?t=9364

+0

谢谢了'self.sendto(数据(地址,端口))' – moonraker