From 153297b795e241d63461fbb99e3d0398c8b27ee7 Mon Sep 17 00:00:00 2001 From: Della Baby <delba753@student.liu.se> Date: Wed, 5 Feb 2020 17:46:50 +0100 Subject: [PATCH] changes for lab1 --- lab1/client.py | 27 +++++++++++++++------------ lab1/dbs/fortune.db | 36 ------------------------------------ lab1/server.py | 11 +++++++++-- 3 files changed, 24 insertions(+), 50 deletions(-) diff --git a/lab1/client.py b/lab1/client.py index 162aee2..7758dad 100644 --- a/lab1/client.py +++ b/lab1/client.py @@ -68,19 +68,20 @@ class DatabaseProxy(object): # # Your code here. # - args = [] msg = { "method": "read" , - "args": args + "args": [] } s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(self.address) - conn = s.makefile(mode="rw") - conn.write(json.dumps(msg) + '\n') - conn.flush() - res = json.loads(conn.readline()) - conn.flush() + worker = s.makefile(mode="rw") + worker.write(json.dumps(msg) + '\n') + worker.flush() + + res = json.loads(worker.readline()) + s.close() + return res pass @@ -94,12 +95,14 @@ class DatabaseProxy(object): } s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(self.address) - conn = s.makefile(mode="rw") - conn.write(json.dumps(msg) + '\n') - conn.flush() - res = json.loads(conn.readline()) - conn.flush() + worker = s.makefile(mode="rw") + worker.write(json.dumps(msg) + '\n') + worker.flush() + + res = json.loads(worker.readline()) + s.close() + if res: print(res) pass diff --git a/lab1/dbs/fortune.db b/lab1/dbs/fortune.db index 0a814b4..9ae637c 100644 --- a/lab1/dbs/fortune.db +++ b/lab1/dbs/fortune.db @@ -3017,39 +3017,3 @@ come at you rapidly. "Style Invitational Report from Week 278" published August 2, 1998 % -TDDD25 -% -distributed systems -% -hai -% -hai -% -hai -% -jhj -% -jkjkj -% -nnn -% -jjj -% -hhh -% -jkdhkjhdf -% -iudyucgdhcn -% -ttttttttttttt -% -jjjjjjjjjjj -% -kkkkkkkkk -% -hhhh -% -jj -% -klooo -% diff --git a/lab1/server.py b/lab1/server.py index 1fe432a..3648815 100644 --- a/lab1/server.py +++ b/lab1/server.py @@ -129,16 +129,23 @@ class Request(threading.Thread): # request_message = json.loads(request) + try: + #print(request_message) if request_message != None and request_message['method'] != None and request_message['args'] != None: + if request_message['method'] == "write" and len(request_message['args']) > 0: + res = json.dumps(self.db_server.write(request_message['args'])) elif request_message['method'] == "read": res = json.dumps(self.db_server.read()) + except Exception as e: + else: - res = json.dumps({"error" : {"name": "Unknown method" , "args": request_message['method']}}) + #res = json.dumps({"error" : {"name": "error method" , "args": request_message['method']}}) else: - res = json.dumps({"error" : {"name": "Unknown method" , "args": request_message['method']}}) + #res = json.dumps({"error" : {"name": "error method" , "args": request_message['method']}}) return res + pass def run(self): -- GitLab