1# Copyright 2019 gRPC authors. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""The example of four ways of data transmission using gRPC in Python.""" 15 16import time 17 18import grpc 19 20import demo_pb2 21import demo_pb2_grpc 22 23__all__ = [ 24 "simple_method", 25 "client_streaming_method", 26 "server_streaming_method", 27 "bidirectional_streaming_method", 28] 29 30SERVER_ADDRESS = "localhost:23333" 31CLIENT_ID = 1 32 33# 中文注释和英文翻译 34# Note that this example was contributed by an external user using Chinese comments. 35# In all cases, the Chinese comment text is translated to English just below it. 36 37 38# 一元模式(在一次调用中, 客户端只能向服务器传输一次请求数据, 服务器也只能返回一次响应) 39# unary-unary(In a single call, the client can only send request once, and the server can 40# only respond once.) 41def simple_method(stub): 42 print("--------------Call SimpleMethod Begin--------------") 43 request = demo_pb2.Request( 44 client_id=CLIENT_ID, request_data="called by Python client" 45 ) 46 response = stub.SimpleMethod(request) 47 print( 48 "resp from server(%d), the message=%s" 49 % (response.server_id, response.response_data) 50 ) 51 print("--------------Call SimpleMethod Over---------------") 52 53 54# 客户端流模式(在一次调用中, 客户端可以多次向服务器传输数据, 但是服务器只能返回一次响应) 55# stream-unary (In a single call, the client can transfer data to the server several times, 56# but the server can only return a response once.) 57def client_streaming_method(stub): 58 print("--------------Call ClientStreamingMethod Begin--------------") 59 60 # 创建一个生成器 61 # create a generator 62 def request_messages(): 63 for i in range(5): 64 request = demo_pb2.Request( 65 client_id=CLIENT_ID, 66 request_data="called by Python client, message:%d" % i, 67 ) 68 yield request 69 70 response = stub.ClientStreamingMethod(request_messages()) 71 print( 72 "resp from server(%d), the message=%s" 73 % (response.server_id, response.response_data) 74 ) 75 print("--------------Call ClientStreamingMethod Over---------------") 76 77 78# 服务端流模式(在一次调用中, 客户端只能一次向服务器传输数据, 但是服务器可以多次返回响应) 79# unary-stream (In a single call, the client can only transmit data to the server at one time, 80# but the server can return the response many times.) 81def server_streaming_method(stub): 82 print("--------------Call ServerStreamingMethod Begin--------------") 83 request = demo_pb2.Request( 84 client_id=CLIENT_ID, request_data="called by Python client" 85 ) 86 response_iterator = stub.ServerStreamingMethod(request) 87 for response in response_iterator: 88 print( 89 "recv from server(%d), message=%s" 90 % (response.server_id, response.response_data) 91 ) 92 93 print("--------------Call ServerStreamingMethod Over---------------") 94 95 96# 双向流模式 (在一次调用中, 客户端和服务器都可以向对方多次收发数据) 97# stream-stream (In a single call, both client and server can send and receive data 98# to each other multiple times.) 99def bidirectional_streaming_method(stub): 100 print( 101 "--------------Call BidirectionalStreamingMethod Begin---------------" 102 ) 103 104 # 创建一个生成器 105 # create a generator 106 def request_messages(): 107 for i in range(5): 108 request = demo_pb2.Request( 109 client_id=CLIENT_ID, 110 request_data="called by Python client, message: %d" % i, 111 ) 112 yield request 113 time.sleep(1) 114 115 response_iterator = stub.BidirectionalStreamingMethod(request_messages()) 116 for response in response_iterator: 117 print( 118 "recv from server(%d), message=%s" 119 % (response.server_id, response.response_data) 120 ) 121 122 print("--------------Call BidirectionalStreamingMethod Over---------------") 123 124 125def main(): 126 with grpc.insecure_channel(SERVER_ADDRESS) as channel: 127 stub = demo_pb2_grpc.GRPCDemoStub(channel) 128 129 simple_method(stub) 130 131 client_streaming_method(stub) 132 133 server_streaming_method(stub) 134 135 bidirectional_streaming_method(stub) 136 137 138if __name__ == "__main__": 139 main() 140