#!/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) print("Entering back-and-forth. You'll get to enter a new message once a message is received. Send SIGINT (use Control-C) to exit") with s: s.sendall(b'Hello there') try: while True: data = s.recv(1024) print(">: ", repr(data)) response = input("$: ") s.sendall(response.encode("UTF-8")) except KeyboardInterrupt: print("Exiting.") if __name__ == "__main__": main()