socket-examples: IPv6 related update for examples to set correct scoped id

The scope id must be present when connecting to IPv6 Local Link
address.
This commit is contained in:
David Cermak
2020-02-04 16:38:23 +01:00
committed by bot
parent 286538e7af
commit 94ded5fb2f
4 changed files with 19 additions and 13 deletions

View File

@@ -12,17 +12,19 @@ import sys
# ----------- Config ----------
PORT = 3333
IP_VERSION = 'IPv4'
IP_VERSION = 'IPv6'
IPV4 = '192.168.0.167'
IPV6 = 'FE80::32AE:A4FF:FE80:5288'
IPV6 = 'fe80:0000:0000:0000:260a:c4ff:fe11:a1e0%wlp1s0'
# -------------------------------
if IP_VERSION == 'IPv4':
host = IPV4
addr = (IPV4, PORT)
family_addr = socket.AF_INET
elif IP_VERSION == 'IPv6':
host = IPV6
family_addr = socket.AF_INET6
for res in socket.getaddrinfo(IPV6, PORT, socket.AF_INET6,
socket.SOCK_DGRAM, socket.SOL_UDP):
af, socktype, proto, canonname, addr = res
else:
print('IP_VERSION must be IPv4 or IPv6')
sys.exit(1)
@@ -37,7 +39,7 @@ except socket.error:
while True:
msg = input('Enter message to send : ')
try:
sock.sendto(msg.encode(), (host, PORT))
sock.sendto(msg.encode(), addr)
reply, addr = sock.recvfrom(128)
if not reply:
break