【问题标题】:How to run gRPC client on one machine and gRPC server on another machine?如何在一台机器上运行 gRPC 客户端,在另一台机器上运行 gRPC 服务器?
【发布时间】:2021-07-31 05:16:27
【问题描述】:

我已经实现了一个计算给定数字平方根的简单函数。 我在 Mac 机器上运行 server.py,在 windows 机器上运行 client.py。两台机器都连接到同一个网络。 当我在同一台机器上运行客户端和服务器时,它可以工作。但是当我在另一台机器上运行客户端时,它会失败并出现以下错误:

Traceback (most recent call last):
  File "client.py", line 17, in <module>
    response = stub.SquareRoot(number)
  File "/Users/username/Library/Python/2.7/lib/python/site-packages/grpc/_channel.py", line 946, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "/Users/username/Library/Python/2.7/lib/python/site-packages/grpc/_channel.py", line 849, in _end_unary_response_blocking
    raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
        status = StatusCode.UNAVAILABLE
        details = "failed to connect to all addresses"
        debug_error_string = "{"created":"@1620574982.491455000","description":"Failed to pick subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc","file_line":5420,"referenced_errors":[{"created":"@1620574982.491453000","description":"failed to connect to all addresses","file":"src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc","file_line":398,"grpc_status":14}]}"

clinet.py

channel = grpc.insecure_channel('192.168.0.54')
stub = calculator_pb2_grpc.CalculatorStub(channel)
number = calculator_pb2.Number(value=25)

server.py

server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

calculator_pb2_grpc.add_CalculatorServicer_to_server(
        CalculatorServicer(), server)

print('Starting server. Listening on port 50051.')
server.add_insecure_port('[::]:50051')
server.start()

try:
    while True:
        time.sleep(86400)
except KeyboardInterrupt:
    server.stop(0)

【问题讨论】:

    标签: python grpc communication


    【解决方案1】:

    客户端需要同时添加端口,比如channel = grpc.insecure_channel('192.168.0.54:50051'),那就试试吧。

    如果还是不行,尝试关闭防火墙,通过telnet检查网络是否正常,如telnet 192.168.0.54 50051

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      相关资源
      最近更新 更多