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.
........
This commit is contained in:
Amaury Forgeot d'Arc 2009-06-23 22:01:54 +00:00
parent 5034624fdd
commit 378314330f
2 changed files with 11 additions and 23 deletions

View File

@ -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()).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 @@ 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

View File

@ -422,11 +422,6 @@ The module :mod:`socket` exports the following constants and functions:
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)