Debian Codegraph @

examples/python/data_transmission/client.py

raw
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
16 import time
17
18 import grpc
19
20 import demo_pb2
21 import demo_pb2_grpc
22
23 __all__ = [
24 'simple_method', 'client_streaming_method', 'server_streaming_method',
25 'bidirectional_streaming_method'
26 ]
27
28 SERVER_ADDRESS = "localhost:23333"
29 CLIENT_ID = 1
30
31 # 中文注释和英文翻译
32 # Note that this example was contributed by an external user using Chinese comments.
33 # In all cases, the Chinese comment text is translated to English just below it.
34
35
36 # 一元模式(在一次调用中, 客户端只能向服务器传输一次请求数据, 服务器也只能返回一次响应)
37 # unary-unary(In a single call, the client can only send request once, and the server can
38 # only respond once.)
39 def simple_method(stub):
40 print("--------------Call SimpleMethod Begin--------------")
41 request = demo_pb2.Request(client_id=CLIENT_ID,
42 request_data="called by Python client")
43 response = stub.SimpleMethod(request)
44 print("resp from server(%d), the message=%s" %
45 (response.server_id, response.response_data))
46 print("--------------Call SimpleMethod Over---------------")
47
48
49 # 客户端流模式(在一次调用中, 客户端可以多次向服务器传输数据, 但是服务器只能返回一次响应)
50 # stream-unary (In a single call, the client can transfer data to the server several times,
51 # but the server can only return a response once.)
52 def client_streaming_method(stub):
53 print("--------------Call ClientStreamingMethod Begin--------------")
54
55 # 创建一个生成器
56 # create a generator
57 def request_messages():
58 for i in range(5):
59 request = demo_pb2.Request(
60 client_id=CLIENT_ID,
61 request_data=("called by Python client, message:%d" % i))
62 yield request
63
64 response = stub.ClientStreamingMethod(request_messages())
65 print("resp from server(%d), the message=%s" %
66 (response.server_id, response.response_data))
67 print("--------------Call ClientStreamingMethod Over---------------")
68
69
70 # 服务端流模式(在一次调用中, 客户端只能一次向服务器传输数据, 但是服务器可以多次返回响应)
71 # unary-stream (In a single call, the client can only transmit data to the server at one time,
72 # but the server can return the response many times.)
73 def server_streaming_method(stub):
74 print("--------------Call ServerStreamingMethod Begin--------------")
75 request = demo_pb2.Request(client_id=CLIENT_ID,
76 request_data="called by Python client")
77 response_iterator = stub.ServerStreamingMethod(request)
78 for response in response_iterator:
79 print("recv from server(%d), message=%s" %
80 (response.server_id, response.response_data))
81
82 print("--------------Call ServerStreamingMethod Over---------------")
83
84
85 # 双向流模式 (在一次调用中, 客户端和服务器都可以向对方多次收发数据)
86 # stream-stream (In a single call, both client and server can send and receive data
87 # to each other multiple times.)
88 def bidirectional_streaming_method(stub):
89 print(
90 "--------------Call BidirectionalStreamingMethod Begin---------------")
91
92 # 创建一个生成器
93 # create a generator
94 def request_messages():
95 for i in range(5):
96 request = demo_pb2.Request(
97 client_id=CLIENT_ID,
98 request_data=("called by Python client, message: %d" % i))
99 yield request
100 time.sleep(1)
101
102 response_iterator = stub.BidirectionalStreamingMethod(request_messages())
103 for response in response_iterator:
104 print("recv from server(%d), message=%s" %
105 (response.server_id, response.response_data))
106
107 print("--------------Call BidirectionalStreamingMethod Over---------------")
108
109
110 def main():
111 with grpc.insecure_channel(SERVER_ADDRESS) as channel:
112 stub = demo_pb2_grpc.GRPCDemoStub(channel)
113
114 simple_method(stub)
115
116 client_streaming_method(stub)
117
118 server_streaming_method(stub)
119
120 bidirectional_streaming_method(stub)
121
122
123 if __name__ == '__main__':
124 main()