import asyncio, socket, sys async def client(src_ip, close_mode): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) src_ip_port = (src_ip, 0) s.bind(src_ip_port) dst_ip_port = ('127.0.0.2', 12345) s.connect(dst_ip_port) print(f'Connected from {src_ip}') print(s) try: (r,w) = await asyncio.open_connection(sock=s) print('<- ', end='', flush=True) in_line = await r.read(100) print(in_line) out_line = b'client' print('-> ', end='', flush=True) w.write(out_line) await w.drain() print(out_line) finally: if 'S' in close_mode: s.close() if 'A' in close_mode: w.close() await w.wait_closed() print('Closed socket') print() async def main(close_mode): await client('127.0.0.3', close_mode) await client('127.0.0.4', close_mode) close_mode = sys.argv[1] asyncio.run(main(close_mode))