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

editing server

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