Commit a0f38c1e authored by Ismail Moumni's avatar Ismail Moumni
Browse files

editing server

parent 5041033a
......@@ -32,13 +32,20 @@ class RdosServer:
data = conn.recv(4096)
query = (json.loads(data.decode('utf-8')))
print("Requete reçu : ", query)
match = verif_param(query)
if match != query:
print('Erreur query : ', match)
conn.send(bytes(json.dumps(match), "utf-8"))
else:
print("ok")
conn.send(bytes(json.dumps(match), "utf-8"))
if(query is not None):
# Sending GENERATORS
if (query["request"] == "generators"):
response = db.db_generators()
conn.send(bytes(json.dumps(response), "utf-8"))
# Inserting in DATABASE
elif (query["Tool"] is not None):
match = verif_param(query)
if match != query:
ins = db_insert(conn, match)
print(ins)
else:
raise Exception("Query doesn't match!")
conn.send(bytes(json.dumps(match), "utf-8"))
# Function query_valwid matches the query send from client side
......@@ -46,26 +53,35 @@ class RdosServer:
# Function input dictionary from client
# Function output boolean true if match else False
def query_valid(data: dict):
if 'parameters' in data:
if (data['parameters'] != '') & (len(data) <= 2):
return True
else:
return False
if data is not None:
if 'parameters' in data:
if (data['parameters'] != '') & (len(data) <= 2):
return True
else:
return False
else:
raise Exception("Dictionnaire Vide!!")
# Function json_loads changes a json file into a Dict
# Function json_loads returns a dictionary
def json_to_dict(js):
data = json.loads(js)
return data
if js is not None:
data = json.loads(js)
return data
else:
raise Exception("Dictionnaire Vide!!")
# Function db_req sends a request to get parameters of tools from database
# Function takes a dictionary for input
# Function return a Dict containing tool parameters and default parameters
def db_req(tool: dict):
tool_dict = db.getParameters(tool)
return tool_dict
if dict is not None:
tool_dict = db.getParameters(tool)
return tool_dict
else:
raise Exception("Dictionnaire Vide!!")
# Function match_dict returns non existing fields in a dict
......@@ -96,37 +112,43 @@ def match_query_dict(biblio: dict, data: dict):
# Function input takes a dictionary
# Function output returns query if it matches with database query keys if not it raises an error
def verif_param(data: dict):
tool = data.keys().strip('[]')
s = db.getParameters(data['tool'])
parameter = {}
query = {}
if s is not None:
gen = s[0]
gen_default = s[1]
parameter = gen_default
query = data.get(tool)
if match_query_dict(gen.get(tool), query):
for a in query:
if query[a] == "":
print(query[a])
query[a] = parameter.get(a)
return query
if data is not None:
tool = data.keys().strip('[]')
s = db.getParameters(data['tool'])
parameter = {}
query = {}
if s is not None:
gen = s[0]
gen_default = s[1]
parameter = gen_default
query = data.get(tool)
if match_query_dict(gen.get(tool), query):
for a in query:
if query[a] == "":
print(query[a])
query[a] = parameter.get(a)
return query
else:
raise Exception("Query Doesn't match")
else:
raise Exception("Query Doesn't match")
raise Exception("Error Database")
else:
raise Exception("Error Database")
raise Exception("Dictionnaire Vide!!")
# Function db_generators returns database generators
# Function takes a dictionary request from client side
# Function returns all database generators or raises an error if request doesn't match
def db_generators(req: dict):
match = {"request": "generators"}
if(req == match):
s = db.get_generators()
return s
if req is not None:
match = {"request": "generators"}
if(req == match):
s = db.get_generators()
return s
else:
raise Exception("Error Database")
else:
raise Exception("Error Database")
raise Exception("Dictionnaire Vide!!")
# Function db_insert send a query to insert in database by socket
......@@ -141,3 +163,8 @@ def db_insert(s: socket, req: dict):
else:
raise Exception("Query Doesn't match")
raise Exception("Invalid Query")
if __name__ == '__main__':
# TEST FOR Docker
RdosServer.server_conn('127.0.0.1', '9393')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment