如何在一台机器上运行 gRPC 客户端,在另一台机器上运行 gRPC 服务器?

时间:2021-05-09 15:51:33

标签: python grpc communication

我实现了一个简单的函数来计算给定数字的平方根。 我在 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)

1 个答案:

答案 0 :(得分:0)

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

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

相关问题