My goal basically is to have this 'memcached' object i've created, which is basically a dictionary with a set of commands that resemble the memchached system, and operate them from a client perspective and actually make some changes to the 'memcached dictionary object'.
First of all, i decided to make a memcached.py script that's basically a Memcached class with some of the commands of the real Memcached system, but as methods.
'''Class built with the intention of simulating a real Memcached system'''
class Memcached:
def __init__(self):
self.storage = {}
"""Storage Commands"""
def set(self, key, value):
self.storage[key] = value
print("STORED")
def add(self,key,value):
if key in self.storage:
print("ERROR")
else:
self.storage[key] = value
print("STORED")
"""Retrieval Commands"""
def get(self, key):
response = print(f'VALUE {key} \n {self.storage[key]} \n END')
return response
This works from the terminal obviously, so this is the farthest i would go as of complexity of the Memcached system(i will add some other functionalities/commands when i resolve the bigger problem)
The bigger problem:
What i'm trying to do now is actually send and receive this memcached object through a localhost socket, so(for example) the client can store and retrieve information from this memcached object that i've created.
These are basically the scripts i have made:
server.py
import socket
import json
import memcached
cache = memcached.Memcached()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', 12345))
s.listen(1)
print('Socket is listening...')
conn, addr = s.accept()
b = b''
with conn:
while True:
data = conn.recv(1024)
b += data
if not data:
break
d = json.loads(b.decode('utf-8'))
args = d.split(' ')
if args[0] == 'set':
cache.set(args[1], args[2])
elif args[0] == 'add':
cache.add(args[1], args[2])
elif args[0] == 'get':
cache.get(args[1], args[2])
else:
print('ERROR')
#control print
print(d)
client.py
import socket
import json
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 12345))
class Client:
def __init__(self):
self.host = 'localhost'
self.port = 12345
def command(self, command, key, value):
cmd = command + ' ' + key + ' ' + value
b = json.dumps(cmd).encode('utf-8')
s.sendall(b)
newClient = Client()
while True:
print('Taking memcached commands...')
cmd = input()
if cmd == 'quit':
break
else:
cmdList = cmd.split()
newClient.command(cmdList[0], cmdList[1], cmdList[2])
EDITED: Updated the scripts with the logic that lets me accede memcached methods from the client side. However now i'm not able to send different commands from the same connection. Is threading neccesary or a simple change in the logic could solve this? For example, in the current state i'm in, i can't use the 'get' command since the dictionary is basically rebuilt after the client script ends.