[Twisted-Python] help with listening to UDP broadcast?
Pantelis Theodosiou
ypercube at gmail.com
Fri Aug 8 18:04:31 MDT 2014
Hi Russell,
In an old script I've written, I see the following differences with your
code:
I hadn't used at all allsetBroadcastAllowed()
but joinGroup() (in startPrtotocol).
And I don't think the 255.255.255.255 will work. You'll need something like
192.168.1.1 or 127.0.0.1
The code (stripped from irrelevant parts) was:
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
from twisted.application.internet import MulticastServer
# Set ports and hosts
ip = '224.0.5.228'
port = 8228
# our interface
interface = '192.168.1.1'
class MulticastClientUDP(DatagramProtocol):
def startProtocol(self):
# Join the multicast group
self.transport.joinGroup(ip, interface)
self.transport.setTTL(255)
def datagramReceived(self, datagram, address):
print "got a UDP broadcast packet"
# set reactor to listen on multicast channels
alertsClient = MulticastClientUDP()
reactor.listenMulticast(port, alertsClient)
reactor.run()
Pantelis Theodosiou
On Fri, Aug 8, 2014 at 11:58 PM, Russell E. Owen <rowen at uw.edu> wrote:
> I've found an example for UDP broadcasting:
> <https://twistedmatrix.com/trac/browser/trunk/docs/projects/core/examples
> /udpbroadcast.py?rev=41516
> <https://twistedmatrix.com/trac/browser/trunk/docs/projects/core/examples/udpbroadcast.py?rev=41516>
> >
>
> However, it combines the sender and receiver in a way that I find
> confusing. I figured out how output UDP broadcast packets, but not how
> to make a client that receives to the packets.
>
> Here is what we have, which doesn't work. Based on a comment on the
> broadcast UDP ticket. I suspect the problem is specifying the broadcast
> address using the interface argument to listenUDP, but I'm not sure. In
> any case, no value for interface that I've tried works:
> -"<broadcast>" (which is what my senders uses) results in
> twisted.internet.error.InvalidAddressError
> -"255.255.255.255" results in twisted.internet.error.CannotListenError:
> Couldn't listen on 255.255.255.255:1235: [Errno 49] Can't assign
> requested address.
> - omitting it results in no packets received.
> I also tried listenMulticast, but it didn't work (and I didn't expect it
> to, based on comments I saw on the ticket for implementing UDP broadcast
> support).
>
> Any hints would be appreciated.
>
> #!/usr/bin/env python2
> """Attempt to listen to UDP broadcasts
> """
> from twisted.internet.protocol import DatagramProtocol
> from twisted.internet import reactor
>
> Port = 1235
>
> class BroadcastUDPClient(DatagramProtocol):
> def startProtocol(self):
> self.transport.setBroadcastAllowed(True)
>
> def datagramReceived(self, datagram, address):
> print "got a UDP broadcast packet"
>
> class UDPListener(object):
> def __init__(self, port):
> self.port = port
> self.broadcastClient = BroadcastUDPClient()
> self.listener = None
> self.startListening()
>
> def startListening(self):
> if self.listener is None:
> interface = "255.255.255.255"
> self.listener = reactor.listenUDP(self.port,
> self.broadcastClient, interface)
>
> def stopListening(self):
> if self.listener is not None:
> self.listener.stopListening()
> self.listener = None
>
> if __name__ == "__main__":
> listener = UDPListener(Port)
> reactor.run()
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20140809/f86c99db/attachment-0002.html>
More information about the Twisted-Python
mailing list