def test_server(self): with RPCServer(1234): with grpc.insecure_channel("localhost:1234") as channel: stub = service_pb2_grpc.RpcServiceStub(channel) req = service_pb2.RequestList() req.request.extend([service_pb2.Request(input="ccc")]) res = stub.resolveBatch(req) self.assertEqual(3, res.response[0].output)
def multiply(a,b): try: channel = grpc.insecure_channel('localhost:4040') stub = service_pb2_grpc.AddServiceStub(channel) req = service_pb2.Request(a=int(a), b = int(b)) response = stub.Multiply(req) return jsonify({ "result": response.result, }) except Exception as e: print(e) return e
def wrapper(*args): request = { 'clazz': func.__module__, 'method': func.__name__, 'args': list(args) } request_json = json.dumps(request, ensure_ascii=False) response = grpc_client.connect(server).handle( service_pb2.Request(request=bytes(request_json.encode('utf-8')), serialize=serialize)) response_json = json.loads(response.response) if response_json.get('status') == 0: return response_json.get('result') elif response_json.get('status') == -1: raise GrpcException(response_json.get('excType'), response_json.get('message')) else: raise Exception('unknown grpc exception')
def flush(self, batch, batchSize): import service_pb2 batchSize.clear() req = service_pb2.RequestList() inputs = batch.read() req.request.extend( [service_pb2.Request(input=item) for item in set(inputs)]) batch.clear() res = self.stub.resolveBatch(req) resolved = { req.request[i].input: res.response[i].output for i in range(0, len(req.request)) } return [(elem, resolved[elem]) for elem in inputs]
def run(server_ip): channel = grpc.insecure_channel('%s:30100' % server_ip) stub = service_pb2_grpc.MyServiceStub(channel) response = stub.handle_request(service_pb2.Request(name='you')) print("received: " + response.message)
def client(): # Configura o canal de comunicacao channel = grpc.insecure_channel('localhost:7777') # Inicializa e configura o stub stub = service_pb2_grpc.FunctionalitiesStub(channel) print("[1] Cadastra nota para um aluno") print("[2] Consulta a nota de um aluno") print("[3] Remove a nota de um aluno") print("[4] Atualiza a nota de um aluno") print( "[5] Consulta as notas e faltas de uma disciplina em um ano/semestre.") print("[6] Consulta de alunos de uma disciplina em um ano/semestre\n") while (True): try: operationId = int(input("Que operação deseja fazer? > ")) except ValueError: print("ERRO: OPERACAO INVALIDA!") continue # Instanciando a estrutura req = service_pb2.Request() req.operationId = operationId # Preenche a estrutura de acordo com a operacao if (operationId >= 1 and operationId <= 6): discCode = input("Codigo da disciplina > ") ano = input("Ano > ") semestre = input("Semestre > ") # Tratando campos vazios if (discCode == '' or ano == '' or semestre == ''): print("ERRO: CAMPOS VAZIOS!") continue req.discCode = str(discCode) req.ano = int(ano) req.semestre = int(semestre) else: print("ERRO: OPERACAO INVALIDA!") continue if (operationId >= 1 and operationId <= 4): ra = input("RA do aluno > ") # Tratando campos vazios if (ra == ''): print("ERRO: CAMPO VAZIO!") continue req.RA = int(ra) if (operationId == 1 or operationId == 4): nota = input("Nota > ") # Tratando campos vazios if (nota == ''): print("ERRO: CAMPO VAZIO!") continue req.nota = float(nota) # Chamada remota res = stub.Execute(req) # Trata resposta do servidor if (res.operationId == operationId): # Sucesso na operacao if (res.status == "1"): if (res.operationId == 2): for aluno in res.alunos: if (aluno.nota == float(-1)): print("Nota: N/A") else: print("Nota:", aluno.nota) if (res.operationId == 5): for aluno in res.alunos: print("\nRA:", aluno.RA) if (aluno.nota == float(-1)): print("Nota: N/A") else: print("Nota:", aluno.nota) print("Faltas:", aluno.faltas) print("--------------------") if (res.operationId == 6): for aluno in res.alunos: print("\nRA:", aluno.RA) print("Nome:", aluno.nome) print("Periodo:", aluno.periodo) print("--------------------") else: print("Operação realizada com sucesso") # Mostra codigo do erro else: print(res.status)
def run(server_ip): channel = grpc.insecure_channel("%s:30101" % server_ip) stub = service_pb2_grpc.MyServiceStub(channel) response = stub.handle_request(service_pb2.Request(name="There!")) print("received from server: " + response.message)