Add TCP chat client implementation
Implement a TCP chat client that connects to a server, sends messages, and receives messages in a separate thread.
This commit is contained in:
1 parent
76a1ca6966
commit
154cb2d2a7
1 file changed
+18
@@ -0,0 +1,18 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import socket, sys, threading
|
||||||
|
|
||||||
|
host = sys.argv[1] if len(sys.argv) > 1 else "127.0.0.1"
|
||||||
|
port = int(sys.argv[2]) if len(sys.argv) > 2 else 9000
|
||||||
|
|
||||||
|
def reader(s):
|
||||||
|
while data := s.recv(1024):
|
||||||
|
print(data.decode(errors="replace"), end="")
|
||||||
|
print("\n[disconnected]")
|
||||||
|
|
||||||
|
with socket.create_connection((host, port)) as s:
|
||||||
|
threading.Thread(target=reader, args=(s,), daemon=True).start()
|
||||||
|
try:
|
||||||
|
for line in sys.stdin:
|
||||||
|
s.sendall(line.encode())
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
Reference in new issue
Block a user