Created
October 15, 2025 15:09
-
-
Save tiagocoutinho/06276061c1dcb87f39ea24ea96d9e196 to your computer and use it in GitHub Desktop.
TCP client CLI with yield example in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import socket | |
| def make_stream(addr): | |
| c = socket.create_connection(("0",5001)) | |
| print("Connected!") | |
| reply = c | |
| while True: | |
| request = yield reply | |
| if not request.strip(): | |
| break | |
| c.sendall(request) | |
| if not (reply := c.recv(1024)): | |
| break | |
| def cli(): | |
| stream = make_stream(("0", 5001)) | |
| prepare = stream.send(None) | |
| while True: | |
| message = input("> ").encode() | |
| reply = stream.send(message) | |
| print(reply.decode()) | |
| cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment