diff options
Diffstat (limited to 'clientinet.py')
| -rwxr-xr-x | clientinet.py | 57 |
1 files changed, 0 insertions, 57 deletions
diff --git a/clientinet.py b/clientinet.py deleted file mode 100755 index f4f4887..0000000 --- a/clientinet.py +++ /dev/null @@ -1,57 +0,0 @@ -#!/usr/bin/env python3 - -import socket -import sys -import threading -import json -import getpass - - -def send(sock, msg): - length = len(msg) - header = length.to_bytes(4, byteorder="big") - totalmsg = header + msg - sock.sendall(totalmsg) - -def receive(sock): - header = recvall(sock, 4) #sock.recv(4) - length = int.from_bytes(header, byteorder="big") - return recvall(sock, length) - -def recvall(sock, length): - chunks = [] - bytes_recd = 0 - while bytes_recd < length: - chunk = sock.recv(min(length - bytes_recd, 4096)) - if chunk == b'': - break - #raise RuntimeError("socket connection broken") - chunks.append(chunk) - bytes_recd = bytes_recd + len(chunk) - return b''.join(chunks) - - - -sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -sock.connect(("localhost", 1234)) - -def listen(): - while True: - d = receive(sock) - if len(d) == 0: - print("Connection closed by server", file=sys.stdout) - return - print(str(d, "utf-8")) - -threading.Thread(target=listen, daemon=True).start() - -if len(sys.argv) >= 2: - name = sys.argv[1] -else: - name = getpass.getuser() -print(name) - -send(sock, bytes(json.dumps(["name", name]), "utf-8")) - -for line in sys.stdin: - send(sock, bytes(json.dumps(["chat", line.strip()]), "utf-8")) |
