-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServidor.py
68 lines (57 loc) · 2.26 KB
/
Servidor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# On this side we have acces to the DataBase (File Methods)
from FileMethods import *
from socket import *
from Classes import *
import pickle
import threading
# creating, binding and listening the server
servidor = socket(AF_INET, SOCK_STREAM)
servidor.bind(("127.0.0.1", 9999))
print("server binded")
servidor.listen()
def usar(clientsocket):
data = ""
while data != "sair":
client = ""
data = clientsocket.recv(4096).decode()
print("MÉTODO ESCOLHIDO: ", data)
if data == "acessaruserinfo":
clientid = clientsocket.recv(4096).decode()
infor = acessaruserinfo(clientid)
clientsocket.send(pickle.dumps(infor))
elif data == "pegarclient":
# construir e enviar cópia do cliente para manuseio a partir da infor recebida
infor = pickle.loads(clientsocket.recv(4096))
print("informação recebida:", infor)
client = Cofre(infor[2]["nome"], infor[2]["cofre"])
# copiando dados
for i in range(len(infor[1])):
if i <= 2:
client.adddados(infor[1][i], 1)
else:
client.adddados(infor[1][i], 0)
# copiando cofre
for i1 in zip(infor[2].keys(), infor[2].values()):
client.adicionarsenha(i1[0], i1[1])
clientsocket.send(pickle.dumps(client))
print("cliente enviado:", client)
elif data == "replace":
client = pickle.loads(clientsocket.recv(4096))
print("cliente recebido\n", client)
replace(client.save())
print("como o file está agora", ler())
clientsocket.send("seu cliente foi substituido".encode())
elif data == "deleteuser":
client = pickle.loads(clientsocket.recv(4096))
print("cliente recebido\n", client)
deleteuser(client.save())
print("como o file está agora", ler())
clientsocket.send("seu cliente foi deletado".encode())
clientsocket.close()
# Try to transform in a MT Server
while True:
print("Aguardando conexão...")
csocket, adrr = servidor.accept()
print("conectado a um cliente ☺")
t = threading.Thread(target=usar, args=(csocket, ))
t.start()