summaryrefslogtreecommitdiffstats
path: root/Demo/sockets
diff options
context:
space:
mode:
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>2009-06-23 22:01:54 (GMT)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>2009-06-23 22:01:54 (GMT)
commit378314330fbe7cb0ca8b81b1e0f0f7f45183c4ff (patch)
tree5c22856f01af8076b76ebb5f35680583246e5acb /Demo/sockets
parent5034624fdd23d6f0df1d3a5a96896ef8400af2fb (diff)
downloadcpython-378314330fbe7cb0ca8b81b1e0f0f7f45183c4ff.zip
cpython-378314330fbe7cb0ca8b81b1e0f0f7f45183c4ff.tar.gz
cpython-378314330fbe7cb0ca8b81b1e0f0f7f45183c4ff.tar.bz2
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. ........
Diffstat (limited to 'Demo/sockets')
-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 d32a9df..b7f8556 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()).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