From ee4bb1a1848accaa2e414fb97dbceb9203417ae5 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Thu, 1 Aug 2024 14:38:24 -0700 Subject: [PATCH] Allow multiple messages by taking user input --- client.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/client.py b/client.py index 9adbabe..d6f07e5 100755 --- a/client.py +++ b/client.py @@ -28,13 +28,17 @@ def main(): 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, world') - data = s.recv(1024) - print('Received', repr(data)) - - if __name__ == "__main__": - main() + 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()