Amaury Forgeot d'Arc avatar Amaury Forgeot d'Arc committed cb3d323

Merged revisions 73537 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
r73537 | amaury.forgeotdarc | 2009-06-23 23:53:46 +0200 (mar., 23 juin 2009) | 3 lines

Remove last remnants of the ipaddr package.
The changes in mcast.py come from the first version of the patch for issue5379.
........

Comments (0)

Files changed (2)

Demo/sockets/mcast.py

 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
     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()).encode('utf-8') + b'\0'
-        s.sendto(data, (group_ip.ip_ext_full, MYPORT))
+        s.sendto(data, (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)
     # 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

Doc/library/socket.rst

 
    Availability: Unix (maybe not all platforms).
 
-   .. seealso::
-
-      :func:`ipaddr.BaseIP.packed`
-         Platform-independent conversion to a packed, binary format.
-
 
 .. function:: inet_ntop(address_family, packed_ip)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.