server.py 4.02 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
Ismail Moumni committed
6
# import 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
        with conn:
Ismail Moumni's avatar
editing    
Ismail Moumni committed
29
            print('Connexion acceptee depuis l IP : ', addr)
Ismail Moumni's avatar
Ismail Moumni committed
30
31
32
33
34
            # Receiving Data from Client
            data = conn.recv(4096)
            query = (json.loads(data.decode('utf-8')))
            print(query)
            if query_valid(query):
Ismail Moumni's avatar
Ismail Moumni committed
35
36
                Tool = __RDOS_Tool__
                dt_js = json.dumps(send_param_client(__RDOS_Dict__, Tool))
Ismail Moumni's avatar
Ismail Moumni committed
37
                print(dt_js)
Ismail Moumni's avatar
Ismail Moumni committed
38
39
40
                conn.send(bytes((dt_js), "utf-8"))


Ismail Moumni's avatar
Ismail Moumni committed
41
# Function query_valwid matches the query send from client side
Ismail Moumni's avatar
Ismail Moumni committed
42
43
44
45
46
#  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:
Ismail Moumni's avatar
Ismail Moumni committed
47
        if (data['parameters'] != '') & (len(data) <= 2):
Ismail Moumni's avatar
Ismail Moumni committed
48
49
50
51
            return True
        return False


Ismail Moumni's avatar
Ismail Moumni committed
52
53
# Function send_param_client send a Dict containing parameters
# needed to add a query to db
Ismail Moumni's avatar
Ismail Moumni committed
54
55
56
57
58
# 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):
Ismail Moumni's avatar
Ismail Moumni committed
59
60
    # req = db.getParameters()
    pass
Ismail Moumni's avatar
Ismail Moumni committed
61
62
63
64
65
66
67
68
69


# 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


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


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
83
        raise ValueError("Error Dict Values ")
Ismail Moumni's avatar
Ismail Moumni committed
84
85
86
87
88
89
90
91
92
93
94


# 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
Ismail Moumni's avatar
Ismail Moumni committed
95
96


Ismail Moumni's avatar
Ismail Moumni committed
97
98
99
def verif_param(data: dict):
    # Tuple
    tool = data.keys().strip('[]')
Ismail Moumni's avatar
Ismail Moumni committed
100
    # s = db.getParameters(data['tool'])
Ismail Moumni's avatar
Ismail Moumni committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    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
Ismail Moumni committed
120
if __name__ == '__main__':
Ismail Moumni's avatar
Ismail Moumni committed
121
    tool = 'Regal'
Ismail Moumni's avatar
Ismail Moumni committed
122
    # 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
123
124
125
126
    # 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())