Skip to content
Permalink
Browse files

Open a new thread for each client so the server can communicate with …

…multiple clients
  • Loading branch information
chelseatroy committed Dec 16, 2019
1 parent d65da14 commit c4dfb0b862a4a905d6207fe0825d0adf263360d9
Showing with 12 additions and 8 deletions.
  1. +11 −5 echo_server.py
  2. +1 −3 key_value_operations.py
@@ -1,4 +1,5 @@
import socket
import threading
from key_value_operations import KeyValueStore

def run_server():
@@ -12,18 +13,22 @@ def run_server():
sock.listen(1)

while True:
print('waiting for a connection')
connection, client_address = sock.accept()
print("connection from " + str(client_address))

try:
print(f"connection from {client_address}")
threading.Thread(target=handle_client, args=(connection, kvs)).start()

def handle_client(connection, kvs):
while True:
print('waiting for a connection')

try:
while True:
operation = connection.recv(1024)

if operation:
string_operation = operation.decode("utf-8")
print(f"received {string_operation}")
print("received " + string_operation)

f = open("commands.txt", "a")
f.write(string_operation + '\n')
@@ -33,12 +38,13 @@ def run_server():
connection.sendall(response.encode('utf-8'))

else:
print(f"no more data from {client_address}")
print("no more data")
break

finally:
connection.close()


def catch_up(key_value_store):
f = open("commands.txt", "r")
log = f.read()
@@ -1,11 +1,9 @@
import json

class KeyValueStore:
def __init__(self):
self.data = {}

def get(self, key):
return self.data[key]
return self.data.get(key, '')

def set(self, key, value):
self.data[key] = value

0 comments on commit c4dfb0b

Please sign in to comment.
You can’t perform that action at this time.