Skip to content

Instantly share code, notes, and snippets.

@tiagocoutinho
Created October 15, 2025 15:09
Show Gist options
  • Select an option

  • Save tiagocoutinho/06276061c1dcb87f39ea24ea96d9e196 to your computer and use it in GitHub Desktop.

Select an option

Save tiagocoutinho/06276061c1dcb87f39ea24ea96d9e196 to your computer and use it in GitHub Desktop.
TCP client CLI with yield example in python
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