我实现了一个简单的函数,它计算给定数字的平方根。我在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)
服务器.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)
客户端需要同时添加端口,如channel = grpc.insecure_channel('192.168.0.54:50051')
,然后尝试。
如果仍然不起作用,请尝试关闭防火墙,并通过telnet检查网络是否正常工作,如telnet 192.168.0.54 50051