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 16from threading import Thread 17from concurrent import futures 18 19import grpc 20import demo_pb2_grpc 21import demo_pb2 22 23__all__ = 'DemoServer' 24SERVER_ADDRESS = 'localhost:23333' 25SERVER_ID = 1 26 27 28class DemoServer(demo_pb2_grpc.GRPCDemoServicer): 29 30 # 一元模式(在一次调用中, 客户端只能向服务器传输一次请求数据, 服务器也只能返回一次响应) 31 # unary-unary(In a single call, the client can only send request once, and the server can 32 # only respond once.) 33 def SimpleMethod(self, request, context): 34 print("SimpleMethod called by client(%d) the message: %s" % 35 (request.client_id, request.request_data)) 36 response = demo_pb2.Response( 37 server_id=SERVER_ID, 38 response_data="Python server SimpleMethod Ok!!!!") 39 return response 40 41 # 客户端流模式(在一次调用中, 客户端可以多次向服务器传输数据, 但是服务器只能返回一次响应) 42 # stream-unary (In a single call, the client can transfer data to the server several times, 43 # but the server can only return a response once.) 44 def ClientStreamingMethod(self, request_iterator, context): 45 print("ClientStreamingMethod called by client...") 46 for request in request_iterator: 47 print("recv from client(%d), message= %s" % 48 (request.client_id, request.request_data)) 49 response = demo_pb2.Response( 50 server_id=SERVER_ID, 51 response_data="Python server ClientStreamingMethod ok") 52 return response 53 54 # 服务端流模式(在一次调用中, 客户端只能一次向服务器传输数据, 但是服务器可以多次返回响应) 55 # unary-stream (In a single call, the client can only transmit data to the server at one time, 56 # but the server can return the response many times.) 57 def ServerStreamingMethod(self, request, context): 58 print("ServerStreamingMethod called by client(%d), message= %s" % 59 (request.client_id, request.request_data)) 60 61 # 创建一个生成器 62 # create a generator 63 def response_messages(): 64 for i in range(5): 65 response = demo_pb2.Response( 66 server_id=SERVER_ID, 67 response_data=("send by Python server, message=%d" % i)) 68 yield response 69 70 return response_messages() 71 72 # 双向流模式 (在一次调用中, 客户端和服务器都可以向对方多次收发数据) 73 # stream-stream (In a single call, both client and server can send and receive data 74 # to each other multiple times.) 75 def BidirectionalStreamingMethod(self, request_iterator, context): 76 print("BidirectionalStreamingMethod called by client...") 77 78 # 开启一个子线程去接收数据 79 # Open a sub thread to receive data 80 def parse_request(): 81 for request in request_iterator: 82 print("recv from client(%d), message= %s" % 83 (request.client_id, request.request_data)) 84 85 t = Thread(target=parse_request) 86 t.start() 87 88 for i in range(5): 89 yield demo_pb2.Response( 90 server_id=SERVER_ID, 91 response_data=("send by Python server, message= %d" % i)) 92 93 t.join() 94 95 96def main(): 97 server = grpc.server(futures.ThreadPoolExecutor()) 98 99 demo_pb2_grpc.add_GRPCDemoServicer_to_server(DemoServer(), server) 100 101 server.add_insecure_port(SERVER_ADDRESS) 102 print("------------------start Python GRPC server") 103 server.start() 104 server.wait_for_termination() 105 106 # If raise Error: 107 # AttributeError: '_Server' object has no attribute 'wait_for_termination' 108 # You can use the following code instead: 109 # import time 110 # while 1: 111 # time.sleep(10) 112 113 114if __name__ == '__main__': 115 main() 116