I've been looking around to find a way to have SocketServer python module to listen on multicast without success.
Anyone managed to do so ?
Any insight will be greatly appreciated !
Thanks
I've been looking around to find a way to have SocketServer python module to listen on multicast without success.
Anyone managed to do so ?
Any insight will be greatly appreciated !
Thanks
The docs (http://docs.python.org/library/socketserver.html) don't make any mention about multicast, and the source code (http://hg.python.org/cpython/file/2.7/Lib/SocketServer.py) doesn't set any socket options you'd expect to see in a multicast listener (e.g. socket.IP_ADD_MEMBERSHIP), so I'd say SocketServer doesn't support multicast.
I assume (you should try to include a code snippet with the error you're getting) you're trying to make a UDPServer and you're getting an error that is something like:
socket.error: [Errno 10049] The requested address is not valid in its context
This is because UDPServer is a subclass of TCPServer, and when a TCPServer is created it calls bind() on the specified address. You're not supposed to bind to a multicast address for listening though (hence the error), you use the IP_ADD_MEMBERSHIP socket option to listen for multicast traffic.
Looks like you may have to roll your own multicast server.
This works. I can verify the IGMP gets sent and multicast is then received. The important thing to note is that if you want multicast, you bind the UDPServer to any, start the thread (so server.socket gets created) and then add the multicast membership.
class MessageListenerUDP(SocketServer.ThreadingMixIn, SocketServer.UDPServer): pass
if LISTEN_UDP:
if MULTICAST: server=MessageListenerUDP(('',PORT), MessageHandlerUDP)
else: server=MessageListenerUDP((LISTEN_UDP,PORT), MessageHandlerUDP)
server_thread=threading.Thread(target=server.serve_forever)
server_thread.start()
if MULTICAST:
server.socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
struct.pack("=4sl", socket.inet_aton(LISTEN_UDP), socket.INADDR_ANY))
Use the UDPServer class:
http://docs.python.org/library/socketserver.html#socketserver-udpserver-example