From 9ea33d915264027f2012334d43cb0355650ffc63 Mon Sep 17 00:00:00 2001 From: pjht Date: Wed, 13 May 2020 16:38:34 -0500 Subject: [PATCH] Add network send/recive code to client --- client.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/client.py b/client.py index 69c0cb2..a1dcb48 100644 --- a/client.py +++ b/client.py @@ -16,6 +16,47 @@ from time import sleep import pprint import blocks +pp=pprint.PrettyPrinter(indent=2) + +def recv_str(sock,print_str=True): + str="" + ch="" + while True: + ch=sock.recv(1).decode("utf-8") + if ch=="\n": + break + str+=ch + if print_str: + pass + # print("Got string: "+str) + return str + +def send_str(sock,str): + # print("Sending string: "+str) + sock.send((str+"\n").encode("utf-8")) + +def recv_hash(sock): + hash={} + len=int(recv_str(sock,False)) + for _ in range(len): + key=recv_str(sock,False) + val=recv_str(sock,False) + hash[key]=val + # print("Got hash: "+pp.pformat(hash)) + return hash + +def recvall(sock): + BUFF_SIZE=4096 + data=b'' + while True: + part=sock.recv(BUFF_SIZE) + data+=part + if len(part)