-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsv3.py
86 lines (71 loc) · 2.47 KB
/
sv3.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import logging
from concurrent import futures
from concurrent.futures import Future
from concurrent.futures import ThreadPoolExecutor
import requests
import threading
import grpc
import balance_pb2
import balance_pb2_grpc
import os
usuarios = ["Sofia", "Eduardo", "Juan"]
contra = "123"
class Balance(balance_pb2_grpc.BalanceServicer):
"""Missing associated documentation comment in .proto file."""
def Balancear(self, request, context):
new_ruta = request.puerto
with grpc.insecure_channel(new_ruta) as channel:
balance_pb2_grpc.BalanceStub(channel)
return balance_pb2.RespuestaCreate(conf="Carga distribuida al servidor %s!" % new_ruta)
def Create2(self, request, context):
nombre_f = request.nombre
ruta_f = request.ruta
path = os.path.join(ruta_f, nombre_f)
f = open(path, "wb")
f.close()
print("Sv3")
return balance_pb2.RespuestaCreate2(conf="El archivo %s fue creado exitosamente!" % request.nombre)
def Read2(self, request, context):
nombre_f = request.nombre
ruta_f = request.ruta
path = os.path.join(ruta_f, nombre_f)
f = open(path, "rb")
t1 = f.readlines()
print("Archivo leido correctamente!")
f.close()
t = []
i = 0
while i < t1.__len__():
temp = t1[i].decode("utf-8")
t.append(temp)
i += 1
# print(t)
i = 0
while i < t.__len__() - 1:
t[i] = t[i].strip("\r\n")
i += 1
# print(t)
for linea in t:
texto = linea
yield balance_pb2.RespuestaRead2(contenido=texto)
print("Sv3")
def Write2(self, request, context):
nombre_f = request.nombre
ruta_f = request.ruta
# print(ruta_f)
path = os.path.join(ruta_f, nombre_f)
f = open(path, "w")
f.write(request.contenido)
f.close()
# print("Archivo modificado correctamente!")
print("Sv3")
return balance_pb2.RespuestaWrite2(conf="Archivo modificado correctamente!")
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=3))
balance_pb2_grpc.add_BalanceServicer_to_server(Balance(), server)
server.add_insecure_port('[::]:50049')
server.start()
server.wait_for_termination()
if __name__ == '__main__':
logging.basicConfig()
serve()