server.py 4.68 KB
Newer Older
Ismail Moumni's avatar
Ismail Moumni committed
1
# authors: Julien DAVID & Ismail MOUMNI
Ismail-Moumni's avatar
Ismail-Moumni committed
2
import socket
Ismail Moumni's avatar
Ismail Moumni committed
3
import json
Ismail Moumni's avatar
Ismail Moumni committed
4
import os
Ismail Moumni's avatar
Ismail Moumni committed
5
import sys
Ismail Moumni's avatar
edit    
Ismail Moumni committed
6
import database.database as db
Ismail Moumni's avatar
Ismail Moumni committed
7
sys.path.append(os.path.realpath('../softwares/'))
Ismail Moumni's avatar
Ismail Moumni committed
8
9


Ismail Moumni's avatar
Ismail Moumni committed
10
__RDOS_Dict__ = {}
Ismail Moumni's avatar
Ismail Moumni committed
11
12
13
14

# tools List
__RDOS_Tool__ = []

Ismail Moumni's avatar
Ismail Moumni committed
15

Ismail Moumni's avatar
Ismail Moumni committed
16
17
18
19
20
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
Ismail Moumni's avatar
Ismail Moumni committed
21
22
23
24
25
26
    def server_conn(address, PORT):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv:
            serv.bind((address, PORT))
            # Listening to 5 CLients
            serv.listen(5)
            # accepting the connection from client and getting client IP
Ismail Moumni's avatar
Ismail Moumni committed
27
            conn, addr = serv.accept()
Ismail Moumni's avatar
Ismail Moumni committed
28
29
30
31
32
33
34
        while True:
            if conn:
                print('Connexion acceptee depuis l IP : ', addr)
                # Receiving Data from Client
                data = conn.recv(4096)
                query = (json.loads(data.decode('utf-8')))
                print(query)
Ismail Moumni's avatar
editing    
Ismail Moumni committed
35
36
37
38
39
40
41
                match = verif_param(query)
                if match == query:
                    print("ok")
                    conn.send(bytes(json.dumps(match), "utf-8"))
                else:
                    print('Erreur query : ')
                    conn.send(bytes(json.dumps(match), "utf-8"))
Ismail Moumni's avatar
Ismail Moumni committed
42
43


Ismail Moumni's avatar
Ismail Moumni committed
44
# Function query_valwid matches the query send from client side
Ismail Moumni's avatar
Ismail Moumni committed
45
#  with the the parameter dict
Ismail Moumni's avatar
Ismail Moumni committed
46
# Function input dictionary from client
Ismail Moumni's avatar
Ismail Moumni committed
47
48
49
# Function output boolean true if match else False
def query_valid(data: dict):
    if 'parameters' in data:
Ismail Moumni's avatar
Ismail Moumni committed
50
        if (data['parameters'] != '') & (len(data) <= 2):
Ismail Moumni's avatar
Ismail Moumni committed
51
            return True
Ismail Moumni's avatar
edit    
Ismail Moumni committed
52
53
        else:
            return False
Ismail Moumni's avatar
Ismail Moumni committed
54
55


Ismail Moumni's avatar
Ismail Moumni committed
56
57
# Function send_param_client send a Dict containing parameters
# needed to add a query to db
Ismail Moumni's avatar
Ismail Moumni committed
58
# Function exmaple s = {"ARGUMENTS","Tools":"List of Tools" }
Ismail Moumni's avatar
Ismail Moumni committed
59
60
# Function input takes dictionary and a list of tools
# Function output returns a new dictionary containing arguments
Ismail Moumni's avatar
Ismail Moumni committed
61
62
# # for db query and list of tools
def send_param_client(__RDOS_Dict__: dict, __RDOS_Tools__: list):
Ismail Moumni's avatar
editing    
Ismail Moumni committed
63
64
    req = db.getParameters()
    return req
Ismail Moumni's avatar
Ismail Moumni committed
65
66
67


# Function json_loads changes a json file into a Dict
Ismail Moumni's avatar
Ismail Moumni committed
68
# Function json_loads returns a dictionary
Ismail Moumni's avatar
Ismail Moumni committed
69
70
71
72
73
def json_to_dict(js):
    data = json.loads(js)
    return data


Ismail Moumni's avatar
Ismail Moumni committed
74
# Function db_req sends a request to get parameters of tools from database
Ismail Moumni's avatar
Ismail Moumni committed
75
# Function takes a dictionary for input
Ismail Moumni's avatar
Ismail Moumni committed
76
77
# Function return a Dict containing tool parameters and default parameters
def db_req(tool: dict):
Ismail Moumni's avatar
edit    
Ismail Moumni committed
78
79
    tool_dict = db.getParameters(tool)
    return tool_dict
Ismail Moumni's avatar
Ismail Moumni committed
80
81


Ismail Moumni's avatar
Ismail Moumni committed
82
83
84
# Function match_query_dict matches two dictionaries
# Function input takes 2 dictionaries to match
# Function output return trur if dictionaries matches else raise a value error exception
Ismail Moumni's avatar
Ismail Moumni committed
85
86
87
88
def match_query_dict(biblio: dict, data: dict):
    if biblio is not None and data is not None:
        return biblio.keys() == data.keys()
    else:
Ismail Moumni's avatar
Ismail Moumni committed
89
        raise ValueError("Error Dict Values ")
Ismail Moumni's avatar
Ismail Moumni committed
90
91


Ismail Moumni's avatar
Ismail Moumni committed
92
93
94
# Function match_generator_dict Matches dictionary with a list
# Function input 2 arguments dictionary and query from Server
# Function output true if dictionary keys match list elements else false
Ismail Moumni's avatar
Ismail Moumni committed
95
96
97
98
99
100
def match_generator_dict(biblio: dict, Genereator: list):
    for a in Genereator:
        for b in biblio.keys():
            if a != b:
                return False
        return True
Ismail Moumni's avatar
Ismail Moumni committed
101
102


Ismail Moumni's avatar
Ismail Moumni committed
103
104
105
# Function verif_param verifies json send from client to server by matching it with database query
# Function input takes a dictionary
# Function output returns query if it matches with database query keys if not it raises an error
Ismail Moumni's avatar
Ismail Moumni committed
106
107
def verif_param(data: dict):
    tool = data.keys().strip('[]')
Ismail Moumni's avatar
edit    
Ismail Moumni committed
108
    s = db.getParameters(data['tool'])
Ismail Moumni's avatar
Ismail Moumni committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
    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("Error Database")


Ismail Moumni's avatar
edit    
Ismail Moumni committed
128
'''
Ismail Moumni's avatar
Ismail Moumni committed
129
if __name__ == '__main__':
Ismail Moumni's avatar
Ismail Moumni committed
130
    tool = 'Regal'
Ismail Moumni's avatar
error    
Ismail Moumni committed
131
132
    # sa = ({'Regal': {'size': '', 'asize': '', 'outputf': '', 'number': '', 'comp': '', 'min': '', 'con': ''}},
    # {'size': '10', 'asize': '2', 'outputf': '', 'number': '1', 'comp': 'yes', 'min': '', 'con': ''})
Ismail Moumni's avatar
Ismail Moumni committed
133
134
135
136
    # print(verif_param(d, data))
    s = {'Regal': {'size': 'qsd', 'asize': '', 'outputf': 'qsd', 'number': 'ss', 'comp': 'qsd', 'min': 'sqsd', 'con': 'qsd'}}
    #  print(s.get('Regal'))
    print(s.keys())
Ismail Moumni's avatar
edit    
Ismail Moumni committed
137
138
139
    data_test1 = '{"parameters":"request"}'
    data_load = json.loads(data_test1)
    print(query_valid(data_load) is True)
Ismail Moumni's avatar
edit    
Ismail Moumni committed
140
'''