~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/howto/listings/udp/MulticastServer.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from twisted.internet.protocol import DatagramProtocol
 
2
from twisted.internet import reactor
 
3
from twisted.application.internet import MulticastServer
 
4
 
 
5
class MulticastServerUDP(DatagramProtocol):
 
6
    def startProtocol(self):
 
7
        print 'Started Listening'
 
8
        # Join a specific multicast group, which is the IP we will respond to
 
9
        self.transport.joinGroup('224.0.0.1')
 
10
 
 
11
    def datagramReceived(self, datagram, address):
 
12
        # The uniqueID check is to ensure we only service requests from
 
13
        # ourselves
 
14
        if datagram == 'UniqueID':
 
15
            print "Server Received:" + repr(datagram)
 
16
            self.transport.write("data", address)
 
17
 
 
18
# Note that the join function is picky about having a unique object
 
19
# on which to call join.  To avoid using startProtocol, the following is
 
20
# sufficient:
 
21
#reactor.listenMulticast(8005, MulticastServerUDP()).join('224.0.0.1')
 
22
 
 
23
# Listen for multicast on 224.0.0.1:8005
 
24
reactor.listenMulticast(8005, MulticastServerUDP())
 
25
reactor.run()