Back to index

python3.2  3.2.2
Functions | Variables
mcast Namespace Reference

Functions

def main
def sender
def receiver

Variables

int MYPORT = 8123
string MYGROUP_4 = '225.0.0.250'
string MYGROUP_6 = 'ff15:7079:7468:6f6e:6465:6d6f:6d63:6173'
int MYTTL = 1

Function Documentation

def mcast.main ( void  )

Definition at line 24 of file mcast.py.

00024 
00025 def main():
00026     group = MYGROUP_6 if "-6" in sys.argv[1:] else MYGROUP_4
00027 
00028     if "-s" in sys.argv[1:]:
00029         sender(group)
00030     else:
00031         receiver(group)
00032 

Here is the call graph for this function:

Here is the caller graph for this function:

def mcast.receiver (   group)

Definition at line 51 of file mcast.py.

00051 
00052 def receiver(group):
00053     # Look up multicast group address in name server and find out IP version
00054     addrinfo = socket.getaddrinfo(group, None)[0]
00055 
00056     # Create a socket
00057     s = socket.socket(addrinfo[0], socket.SOCK_DGRAM)
00058 
00059     # Allow multiple copies of this program on one machine
00060     # (not strictly needed)
00061     s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
00062 
00063     # Bind it to the port
00064     s.bind(('', MYPORT))
00065 
00066     group_bin = socket.inet_pton(addrinfo[0], addrinfo[4][0])
00067     # Join group
00068     if addrinfo[0] == socket.AF_INET: # IPv4
00069         mreq = group_bin + struct.pack('=I', socket.INADDR_ANY)
00070         s.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
00071     else:
00072         mreq = group_bin + struct.pack('@I', 0)
00073         s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)
00074 
00075     # Loop, printing any data we receive
00076     while True:
00077         data, sender = s.recvfrom(1500)
00078         while data[-1:] == '\0': data = data[:-1] # Strip trailing \0's
00079         print(str(sender) + '  ' + repr(data))
00080 

Here is the call graph for this function:

Here is the caller graph for this function:

def mcast.sender (   group)

Definition at line 33 of file mcast.py.

00033 
00034 def sender(group):
00035     addrinfo = socket.getaddrinfo(group, None)[0]
00036 
00037     s = socket.socket(addrinfo[0], socket.SOCK_DGRAM)
00038 
00039     # Set Time-to-live (optional)
00040     ttl_bin = struct.pack('@i', MYTTL)
00041     if addrinfo[0] == socket.AF_INET: # IPv4
00042         s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin)
00043     else:
00044         s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl_bin)
00045 
00046     while True:
00047         data = repr(time.time()).encode('utf-8') + b'\0'
00048         s.sendto(data, (addrinfo[4][0], MYPORT))
00049         time.sleep(1)
00050 

Here is the caller graph for this function:


Variable Documentation

string mcast.MYGROUP_4 = '225.0.0.250'

Definition at line 15 of file mcast.py.

string mcast.MYGROUP_6 = 'ff15:7079:7468:6f6e:6465:6d6f:6d63:6173'

Definition at line 16 of file mcast.py.

int mcast.MYPORT = 8123

Definition at line 14 of file mcast.py.

Definition at line 17 of file mcast.py.