[Python-checkins] r73539 - in python/branches/py3k: Demo/sockets/mcast.py Doc/library/socket.rst

amaury.forgeotdarc python-checkins at python.org
Wed Jun 24 00:01:55 CEST 2009


Author: amaury.forgeotdarc
Date: Wed Jun 24 00:01:54 2009
New Revision: 73539
Log:
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.
........
Modified:
 python/branches/py3k/ (props changed)
 python/branches/py3k/Demo/sockets/mcast.py
 python/branches/py3k/Doc/library/socket.rst
Modified: python/branches/py3k/Demo/sockets/mcast.py
==============================================================================
--- python/branches/py3k/Demo/sockets/mcast.py	(original)
+++ python/branches/py3k/Demo/sockets/mcast.py	Wed Jun 24 00:01:54 2009
@@ -14,7 +14,6 @@
 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 @@
 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)
@@ -68,12 +60,13 @@
 # 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
Modified: python/branches/py3k/Doc/library/socket.rst
==============================================================================
--- python/branches/py3k/Doc/library/socket.rst	(original)
+++ python/branches/py3k/Doc/library/socket.rst	Wed Jun 24 00:01:54 2009
@@ -422,11 +422,6 @@
 
 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)
 


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /