#!/usr/bin/env python3 import argparse import logging import socket LOGGER = logging.Logger(__name__) 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() addresses = [addr for addr in socket.getaddrinfo(host=args.host, port=args.port, family=socket.AF_INET6, type=socket.SOCK_STREAM, proto=0, flags=0)] if not addresses: raise ValueError("Couldn't find ipv6 address for source %s" % source_ip) if len(addresses) > 1: raise ValueError("Not sure which address you want") address = addresses[0] logging.info("Binding %s", address) bind_socket(*address) def bind_socket(family, type_, proto, canonname, sockaddr): sock = socket.socket( family=family, type=type_, proto=proto, ) sock.bind(sockaddr) LOGGER.info("Bound %s", sockaddr) try: sock.listen(1) except OSError as msg: sock.close() return conn, addr = sock.accept() with conn: LOGGER.info("Connected by %s", addr) while True: data = conn.recv(1024) if not data: break conn.send(data) if __name__ == "__main__": main()