def req_stream_rcp(stub, person, text): name = datahash_pb2.Text(data=person) mesg = datahash_pb2.Text(data=text) result_list = [name, mesg] text_iterator = generator(result_list) response = stub.input_stream(text_iterator) print(response.data)
def bidirect_stream_rcp(stub, person, text): name = datahash_pb2.Text(data=person) mesg = datahash_pb2.Text(data=text) result_list = [name, mesg] text_iterator = generator(result_list) response = stub.dual_stream(text_iterator) for resp in response: print(resp.data)
def simle_rcp(stub, text): to_hello = datahash_pb2.Text(data=text) response = stub.hello(to_hello) print(response.data) to_md5 = datahash_pb2.Text(data=text) response = stub.hash_md5(to_md5) print(response.data) to_sha256 = datahash_pb2.Text(data=text) response = stub.hash_sha256(to_sha256) print(response.data)
def get_image(stub, text): path = datahash_pb2.Text(data=text) response = stub.get_image(path) save_path = '/home/alex/PycharmProjects/usegrpc/client_img/icon.jpg' with open(save_path, 'wb') as image: image.write(response.img) img = plt.imread(save_path) plt.imshow(img) plt.show()
def hash_md5(self, request, context): response = datahash_pb2.Text() print(MessageToDict(request)) print(MessageToJson(request)) json_obj = json.loads(MessageToJson(request)) with open('data.json', 'w') as file: json.dump(json_obj, file) response.data = datahash.hash_md5(request.data) return response
def input_stream(self, request_iterator, context): point_count = 0 prev_point = None points = [] for point in request_iterator: point_count += 1 points.append(point.data) prev_point = point result = datahash.hello_person(points[0], points[1]) return datahash_pb2.Text(data=result)
def stream_text(self, request, context): hello = datahash.hello(request.data) hash_md5 = datahash.hash_md5(request.data) sha256 = datahash.hash_sha256(request.data) result = [hello, hash_md5, sha256] feature_list = [] for item in result: feature = datahash_pb2.Text(data=item) feature_list.append(feature) for feature in feature_list: yield feature
def dual_stream(self, request_iterator, context): point_count = 0 prev_point = None points = [] for point in request_iterator: point_count += 1 points.append(point.data) prev_point = point hello = datahash.hello_person(points[0], points[1]) hash_md5 = datahash.hash_md5(points[1]) sha256 = datahash.hash_sha256(points[1]) result = [hello, hash_md5, sha256] feature_list = [] for item in result: feature = datahash_pb2.Text(data=item) feature_list.append(feature) for feature in feature_list: yield feature
def hello(self, request, context): response = datahash_pb2.Text() response.data = datahash.hello(request.data) return response
def hash_sha256(self, request, context): response = datahash_pb2.Text() response.data = datahash.hash_sha256(request.data) return response
def resp_stream_rcp(stub, text): to_list = datahash_pb2.Text(data=text) items = stub.stream_text(to_list) for item in items: print(item.data)