summaryrefslogtreecommitdiffstats
path: root/Demo/sockets/mcast.py
diff options
context:
space:
mode:
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>2009-06-23 21:53:46 (GMT)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>2009-06-23 21:53:46 (GMT)
commit388637d373807b82cdd4fc04cf46f0b81f162768 (patch)
treea6a2b347a5a20cbfea3b8dc00c0386f41002e7ce /Demo/sockets/mcast.py
parent0115e098daa3851f1e2eb44f277d4cd9101e61b5 (diff)
downloadcpython-388637d373807b82cdd4fc04cf46f0b81f162768.zip
cpython-388637d373807b82cdd4fc04cf46f0b81f162768.tar.gz
cpython-388637d373807b82cdd4fc04cf46f0b81f162768.tar.bz2
Remove last remnants of the ipaddr package.
The changes in mcast.py come from the first version of the patch for issue5379.
Diffstat (limited to 'Demo/sockets/mcast.py')
-rwxr-xr-xDemo/sockets/mcast.py29
1 files changed, 11 insertions, 18 deletions
diff --git a/Demo/sockets/mcast.py b/Demo/sockets/mcast.py
index 25fde79..1873959 100755
--- a/Demo/sockets/mcast.py
+++ b/Demo/sockets/mcast.py
@@ -14,7 +14,6 @@ MYGROUP_4 = '225.0.0.250'
MYGROUP_6 = 'ff15:7079:7468:6f6e:6465:6d6f:6d63:6173'
MYTTL = 1 # Increase to reach other networks
-import ipaddr
import time
import struct
import socket
@@ -28,38 +27,31 @@ def main():
else:
receiver(group)
-def _sockfam(ip):
- """Returns the family argument of socket.socket"""
- if ip.version == 4:
- return socket.AF_INET
- elif ip.version == 6:
- return socket.AF_INET6
- else:
- raise ValueError('IPv' + ip.version + ' is not supported')
def sender(group):
- group_ip = ipaddr.IP(group)
+ addrinfo = socket.getaddrinfo(group, None)[0]
- s = socket.socket(_sockfam(group_ip), socket.SOCK_DGRAM)
+ s = socket.socket(addrinfo[0], socket.SOCK_DGRAM)
# Set Time-to-live (optional)
ttl_bin = struct.pack('@i', MYTTL)
- if group_ip.version == 4:
+ if addrinfo[0] == socket.AF_INET: # IPv4
s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin)
else:
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl_bin)
while True:
data = repr(time.time())
- s.sendto(data + '\0', (group_ip.ip_ext_full, MYPORT))
+ s.sendto(data + '\0', (addrinfo[4][0], MYPORT))
time.sleep(1)
def receiver(group):
- group_ip = ipaddr.IP(group)
+ # Look up multicast group address in name server and find out IP version
+ addrinfo = socket.getaddrinfo(group, None)[0]
# Create a socket
- s = socket.socket(_sockfam(group_ip), socket.SOCK_DGRAM)
+ s = socket.socket(addrinfo[0], socket.SOCK_DGRAM)
# Allow multiple copies of this program on one machine
# (not strictly needed)
@@ -68,12 +60,13 @@ def receiver(group):
# Bind it to the port
s.bind(('', MYPORT))
+ group_bin = socket.inet_pton(addrinfo[0], addrinfo[4][0])
# Join group
- if group_ip.version == 4: # IPv4
- mreq = group_ip.packed + struct.pack('=I', socket.INADDR_ANY)
+ if addrinfo[0] == socket.AF_INET: # IPv4
+ mreq = group_bin + struct.pack('=I', socket.INADDR_ANY)
s.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
else:
- mreq = group_ip.packed + struct.pack('@I', 0)
+ mreq = group_bin + struct.pack('@I', 0)
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)
# Loop, printing any data we receive