ipv6-test/client.py

41 lines
886 B
Python
Raw Normal View History

2024-08-01 14:13:48 -07:00
#!/usr/bin/env python3
import argparse
import logging
import socket
import sys
def main():
parser = argparse.ArgumentParser()
parser.add_argument("host", help="The IPv6 address of the host to connect to.")
parser.add_argument("--port", "-p", default=50007, help="The port to connect to.")
args = parser.parse_args()
s = None
for res in socket.getaddrinfo(args.host, args.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except OSError as msg:
s = None
continue
try:
s.connect(sa)
except OSError as msg:
s.close()
s = None
continue
break
if s is None:
print('could not open socket')
sys.exit(1)
with s:
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
if __name__ == "__main__":
main()
if __name__ == "__main__":
main()