Commit 268f39cc authored by Ismail Moumni's avatar Ismail Moumni
Browse files

adding Json Functions

parent 1f9f00f1
Pipeline #661 failed with stage
in 14 seconds
# authors: Julien DAVID & Ismail MOUMNI
import socket
import json
import sys
import os
import sys
# import database as db
sys.path.append(os.path.realpath('../softwares/'))
# from database.database import getParameters
# SERVER PORT
__RDOS_Port__ = 9393
# Dictionnary
# Need to connect to MYSQL DB
__RDOS_Dict__ = {"ismail":"moumni"}
# tools List
__RDOS_Tool__ = ["name"]
__RDOS_Dict__ = {"ismail": "moumni"}
# tools List
__RDOS_Tool__ = []
class server:
# Creating Socket IP4 TCP
class RdosServer:
# Function server_conn creates a socket and listens on port 9393
# Function input address : SERVER IP ADRESS & Port server Port
# Function output query send from client to server
def server_conn(address, PORT):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv:
serv.bind((address, PORT))
......@@ -25,7 +26,7 @@ class server:
serv.listen(5)
print("ok")
# accepting the connection from client and getting client IP
conn, addr = serv.accept()
conn, addr = serv.accept()
print("connexion accepted")
with conn:
print('Connexion acceptée depuis l IP : ', addr)
......@@ -34,34 +35,110 @@ class server:
query = (json.loads(data.decode('utf-8')))
print(query)
if query_valid(query):
dt_js = json.dumps(send_param_client(__RDOS_Dict__, __RDOS_Tool__))
Tool = __RDOS_Tool__
dt_js = json.dumps(send_param_client(__RDOS_Dict__, Tool))
print(dt_js)
conn.send(bytes((dt_js),"utf-8"))
serv.close()
# Function query_valid matches the query send from client side
# with the the parameter dict
# Function input dictionnary from client
# Function output boolean true if match else False
def query_valid(data: dict):
if 'parameters' in data:
if (data['parameters'] == 'request') & (len(data) < 2):
return True
return False
# Function send_param_client send a dictionnary containing parameters
# needed to add a query to db
# Function exmaple s = {"ARGUMENTS","Tools":"List of Tools" }
# Function input takes dictionnary and a list of tools
# Function output returns a new dictionnary containing arguments
# # for db query and list of tools
def send_param_client(__RDOS_Dict__: dict, __RDOS_Tools__: list):
dict_cl = {}
if len(__RDOS_Dict__) > 0:
dict_cl["outils"] = (__RDOS_Tools__)
s = {**__RDOS_Dict__, **dict_cl}
return s
conn.send(bytes((dt_js), "utf-8"))
# Function query_valid matches the query send from client side
# with the the parameter dict
# Function input dictionnary from client
# Function output boolean true if match else False
def query_valid(data: dict):
if 'parameters' in data:
if (data['parameters'] == 'request') & (len(data) < 2):
return True
return False
# Function send_param_client send a dictionnary containing parameters
# needed to add a query to db
# Function exmaple s = {"ARGUMENTS","Tools":"List of Tools" }
# Function input takes dictionnary and a list of tools
# Function output returns a new dictionnary containing arguments
# # for db query and list of tools
def send_param_client(__RDOS_Dict__: dict, __RDOS_Tools__: list):
dict_cl = {}
if len(__RDOS_Dict__) > 0:
dict_cl["outils"] = (__RDOS_Tools__)
s = {**__RDOS_Dict__, **dict_cl}
return s
# Function json_loads changes a json file into a Dict
# Function json_loads returns a Dictinnary
def json_to_dict(js):
data = json.loads(js)
return data
def db_req(tool: str):
tool_dict = db.getParameters(tool)
return tool_dict
def verif_param(data: dict):
# Tuple
# s = db.getParameters(data['tool'])
s = ({"id": "", "name": ""}, {})
if s is not None:
try:
gen = s[0]
gen_default = s[1]
if match_query_dict(gen, data):
for a in data:
if data[a] == "":
data[a] = gen_default[a]
return data
except Exception as msg:
print("Error : " % msg)
else:
raise Exception("Error from DB")
def match_query_dict(biblio: dict, data: dict):
if biblio is not None and data is not None:
return biblio.keys() == data.keys()
else:
raise ValueError("Error Dict Value ")
# Function match_generator_dict Matches Dictionnary with a list
# Function input 2 arguments Dictionnary and query from Server
# Function output true if dictionnary keys match list elements else false
def match_generator_dict(biblio: dict, Genereator: list):
for a in Genereator:
for b in biblio.keys():
if a != b:
return False
return True
if __name__ == '__main__':
data = {
"internet": "internet",
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
data1 = {"ll": "s", "ismail": "ss"}
print(match_query_dict(data, data))
print(verif_param({"name": ""}))
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