diff --git a/examples/python/data_transmission/alts_client.py b/examples/python/data_transmission/alts_client.py index 4595e249b38..a1dc8ec558e 100644 --- a/examples/python/data_transmission/alts_client.py +++ b/examples/python/data_transmission/alts_client.py @@ -1,4 +1,4 @@ -# Copyright 2019 gRPC authors. +# Copyright 2020 gRPC authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -11,107 +11,24 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -"""The example of four ways of data transmission using gRPC in Python.""" +"""The example of using ALTS credentials to setup gRPC client.""" import time import grpc +import client import demo_pb2_grpc import demo_pb2 SERVER_ADDRESS = "localhost:23333" -CLIENT_ID = 1 - -# 中文注释和英文翻译 -# Note that this example was contributed by an external user using Chinese comments. -# In all cases, the Chinese comment text is translated to English just below it. - - -# 一元模式(在一次调用中, 客户端只能向服务器传输一次请求数据, 服务器也只能返回一次响应) -# unary-unary(In a single call, the client can only send request once, and the server can -# only respond once.) -def simple_method(stub): - print("--------------Call SimpleMethod Begin--------------") - request = demo_pb2.Request(client_id=CLIENT_ID, - request_data="called by Python client") - response = stub.SimpleMethod(request) - print("resp from server(%d), the message=%s" % - (response.server_id, response.response_data)) - print("--------------Call SimpleMethod Over---------------") - - -# 客户端流模式(在一次调用中, 客户端可以多次向服务器传输数据, 但是服务器只能返回一次响应) -# stream-unary (In a single call, the client can transfer data to the server several times, -# but the server can only return a response once.) -def client_streaming_method(stub): - print("--------------Call ClientStreamingMethod Begin--------------") - - # 创建一个生成器 - # create a generator - def request_messages(): - for i in range(5): - request = demo_pb2.Request( - client_id=CLIENT_ID, - request_data=("called by Python client, message:%d" % i)) - yield request - - response = stub.ClientStreamingMethod(request_messages()) - print("resp from server(%d), the message=%s" % - (response.server_id, response.response_data)) - print("--------------Call ClientStreamingMethod Over---------------") - - -# 服务端流模式(在一次调用中, 客户端只能一次向服务器传输数据, 但是服务器可以多次返回响应) -# unary-stream (In a single call, the client can only transmit data to the server at one time, -# but the server can return the response many times.) -def server_streaming_method(stub): - print("--------------Call ServerStreamingMethod Begin--------------") - request = demo_pb2.Request(client_id=CLIENT_ID, - request_data="called by Python client") - response_iterator = stub.ServerStreamingMethod(request) - for response in response_iterator: - print("recv from server(%d), message=%s" % - (response.server_id, response.response_data)) - - print("--------------Call ServerStreamingMethod Over---------------") - - -# 双向流模式 (在一次调用中, 客户端和服务器都可以向对方多次收发数据) -# stream-stream (In a single call, both client and server can send and receive data -# to each other multiple times.) -def bidirectional_streaming_method(stub): - print( - "--------------Call BidirectionalStreamingMethod Begin---------------") - - # 创建一个生成器 - # create a generator - def request_messages(): - for i in range(5): - request = demo_pb2.Request( - client_id=CLIENT_ID, - request_data=("called by Python client, message: %d" % i)) - yield request - time.sleep(1) - - response_iterator = stub.BidirectionalStreamingMethod(request_messages()) - for response in response_iterator: - print("recv from server(%d), message=%s" % - (response.server_id, response.response_data)) - - print("--------------Call BidirectionalStreamingMethod Over---------------") - def main(): with grpc.secure_channel(SERVER_ADDRESS, credentials=grpc.alts_channel_credentials()) as channel: stub = demo_pb2_grpc.GRPCDemoStub(channel) - - simple_method(stub) - - client_streaming_method(stub) - - server_streaming_method(stub) - - bidirectional_streaming_method(stub) + client.simple_method(stub) + client.client_streaming_method(stub) + client.server_streaming_method(stub) + client.bidirectional_streaming_method(stub) if __name__ == '__main__': diff --git a/examples/python/data_transmission/alts_server.py b/examples/python/data_transmission/alts_server.py index 05c2dc34ac1..29a8417152e 100644 --- a/examples/python/data_transmission/alts_server.py +++ b/examples/python/data_transmission/alts_server.py @@ -11,7 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -"""The example of four ways of data transmission using gRPC in Python.""" +"""The example of using ALTS credentials to setup gRPC server in python.""" from threading import Thread from concurrent import futures @@ -19,95 +19,18 @@ from concurrent import futures import grpc import demo_pb2_grpc import demo_pb2 +import server SERVER_ADDRESS = 'localhost:23333' -SERVER_ID = 1 - - -class DemoServer(demo_pb2_grpc.GRPCDemoServicer): - - # 一元模式(在一次调用中, 客户端只能向服务器传输一次请求数据, 服务器也只能返回一次响应) - # unary-unary(In a single call, the client can only send request once, and the server can - # only respond once.) - def SimpleMethod(self, request, context): - print("SimpleMethod called by client(%d) the message: %s" % - (request.client_id, request.request_data)) - response = demo_pb2.Response( - server_id=SERVER_ID, - response_data="Python server SimpleMethod Ok!!!!") - return response - - # 客户端流模式(在一次调用中, 客户端可以多次向服务器传输数据, 但是服务器只能返回一次响应) - # stream-unary (In a single call, the client can transfer data to the server several times, - # but the server can only return a response once.) - def ClientStreamingMethod(self, request_iterator, context): - print("ClientStreamingMethod called by client...") - for request in request_iterator: - print("recv from client(%d), message= %s" % - (request.client_id, request.request_data)) - response = demo_pb2.Response( - server_id=SERVER_ID, - response_data="Python server ClientStreamingMethod ok") - return response - - # 服务端流模式(在一次调用中, 客户端只能一次向服务器传输数据, 但是服务器可以多次返回响应) - # unary-stream (In a single call, the client can only transmit data to the server at one time, - # but the server can return the response many times.) - def ServerStreamingMethod(self, request, context): - print("ServerStreamingMethod called by client(%d), message= %s" % - (request.client_id, request.request_data)) - - # 创建一个生成器 - # create a generator - def response_messages(): - for i in range(5): - response = demo_pb2.Response( - server_id=SERVER_ID, - response_data=("send by Python server, message=%d" % i)) - yield response - - return response_messages() - - # 双向流模式 (在一次调用中, 客户端和服务器都可以向对方多次收发数据) - # stream-stream (In a single call, both client and server can send and receive data - # to each other multiple times.) - def BidirectionalStreamingMethod(self, request_iterator, context): - print("BidirectionalStreamingMethod called by client...") - - # 开启一个子线程去接收数据 - # Open a sub thread to receive data - def parse_request(): - for request in request_iterator: - print("recv from client(%d), message= %s" % - (request.client_id, request.request_data)) - - t = Thread(target=parse_request) - t.start() - - for i in range(5): - yield demo_pb2.Response( - server_id=SERVER_ID, - response_data=("send by Python server, message= %d" % i)) - - t.join() def main(): - server = grpc.server(futures.ThreadPoolExecutor()) - - demo_pb2_grpc.add_GRPCDemoServicer_to_server(DemoServer(), server) - - server.add_secure_port(SERVER_ADDRESS, server_credentials=grpc.alts_server_credentials()) + svr = grpc.server(futures.ThreadPoolExecutor()) + demo_pb2_grpc.add_GRPCDemoServicer_to_server(server.DemoServer(), svr) + svr.add_secure_port(SERVER_ADDRESS, server_credentials=grpc.alts_server_credentials()) print("------------------start Python GRPC server with ALTS encryption") - server.start() - server.wait_for_termination() - - # If raise Error: - # AttributeError: '_Server' object has no attribute 'wait_for_termination' - # You can use the following code instead: - # import time - # while 1: - # time.sleep(10) + svr.start() + svr.wait_for_termination() if __name__ == '__main__':