Skip to content
Permalink
Browse files

Move KeyValueStore into an object

  • Loading branch information
chelseatroy committed Dec 16, 2019
1 parent a52bb6d commit 18e36be17138e902322187983ee7f18f3560035f
Showing with 50 additions and 72 deletions.
  1. +1 −6 echo_client.py
  2. +23 −42 echo_server.py
  3. +26 −24 key_value_operations.py
@@ -1,28 +1,23 @@
import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('localhost', 10000)
print(f"connecting to {server_address[0]} port {server_address[1]}")
sock.connect(server_address)

while True:
try:
# Send data
message = input("Type your message:\n")
print(f"sending {message}")

sock.sendall(message.encode('utf-8'))

# Look for the response
amount_received = 0
amount_expected = len(message)

while amount_received < amount_expected:
data = sock.recv(16)
data = sock.recv(1024)
amount_received += len(data)
print(f"received {data}")
except:
@@ -1,71 +1,52 @@
import socket
import sys

data = {}


def get(key):
return data[key]

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

def delete(key):
del data[key]

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

command, key, value = 0, 1, 2
operands = string_operation.split(" ")

response = "Sorry, I don't understand that command."

if operands[command] == "get":
response = get(operands[key])
elif operands[command] == "set":
set(operands[key], operands[value])
response = f"key {operands[key]} set to {operands[value]}"
elif operands[command] == "delete":
delete(operands[key])
response = f"key {key} deleted"
elif operands[command] == "show":
response = str(data)
else:
pass

return response

from key_value_operations import KeyValueStore

def run_server():
kvs = KeyValueStore()
catch_up(kvs)
server_address = ('localhost', 10000)
print(f"starting up on {server_address[0]} port {server_address[1]}")

sock = socket.socket()
sock.bind(server_address)
sock.listen(1)

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

try:
print(f"connection from {client_address}")

# Receive the data in small chunks and retransmit it
while True:
operation = connection.recv(16)
operation = connection.recv(1024)

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

f = open("commands.txt", "a")
f.write(string_operation + '\n')
f.close()

response = kvs.execute(string_operation)
connection.sendall(response.encode('utf-8'))

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

finally:
# Clean up the connection
connection.close()

def catch_up(key_value_store):
f = open("commands.txt", "r")
log = f.read()
f.close()

for command in log.split('\n'):
key_value_store.execute(command)



run_server()
@@ -1,34 +1,36 @@
import json

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

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

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

def delete(key):
del data[key]
def delete(self, key):
del self.data[key]

def execute(string_operation):
command, key, value = 0, 1, 2
operands = string_operation.split(" ")
def execute(self, string_operation):
command, key, value = 0, 1, 2
operands = string_operation.split(" ")

response = "Sorry, I don't understand that command."
response = "Sorry, I don't understand that command."

if operands[command] == "get":
response = get(operands[key])
elif operands[command] == "set":
set(operands[key], operands[value])
response = f"key {operands[key]} set to {operands[value]}"
elif operands[command] == "delete":
delete(operands[key])
response = f"key {key} deleted"
elif operands[command] == "show":
response = str(data)
else:
pass
if operands[command] == "get":
response = self.get(operands[key])
elif operands[command] == "set":
self.set(operands[key], operands[value])
response = f"key {operands[key]} set to {operands[value]}"
elif operands[command] == "delete":
self.delete(operands[key])
response = f"key {key} deleted"
elif operands[command] == "show":
response = str(self.data)
else:
pass


return response
return response

0 comments on commit 18e36be

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