client.py 4.7 KB

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