4

我正在尝试从 Python 客户端调用在 .Net Core 项目上运行的 GRPC 服务器。

当针对localhost:5001它运行时工作正常,但是从同一网络内针对机器的实际 IP 运行192.168.1.230:5001它不起作用并且我得到一个错误DNS resolution failed

我已经下载了 SSL 证书,目前正在将其作为文件从客户端读取。它在对抗时有效,localhost所以我认为这不是问题。

有没有更好的方法来进行这种让客户端在单独的设备上运行但与服务器在同一网络上运行的测试?在开发期间在外部托管 GRPC 服务器似乎并不是最好的解决方案。

Python代码:

import grpc
import datamessage_pb2 as datamessage
import datamessage_pb2_grpc as datamessageService


def main():
    print("Calling grpc server")
    with open("localhost.cer", "rb") as file:
        cert = file.read()
    credentials = grpc.ssl_channel_credentials(cert)
    channel = grpc.secure_channel("https://192.168.1.230:5001", credentials)
    # channel = grpc.secure_channel("localhost:5001", credentials)
    stub = datamessageService.StationDataHandlerStub(channel)
    request = datamessage.StationDataModel(
        temperature=22.3, humidity=13.3, soilMoisture=35.0)

    result = stub.RegisterNewStationData(request)
    print(result)


main()

中的服务器设置Program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseUrls("https://*:5001");
                    webBuilder.UseStartup<Startup>();
                });

防火墙中的设置: 在此处输入图像描述

追溯:

grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
        status = StatusCode.UNAVAILABLE
        details = "DNS resolution failed"
        debug_error_string = "{"created":"@1576101634.549000000","description":"Failed to pick subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc","file_line":3934,"referenced_errors":[{"created":"@1576101634.549000000","description":"Resolver transient failure","file":"src/core/ext/filters/client_channel/resolving_lb_policy.cc","file_line":262,"referenced_errors":[{"created":"@1576101634.549000000","description":"DNS resolution failed","file":"src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc","file_line":202,"grpc_status":14,"referenced_errors":[{"created":"@1576101634.549000000","description":"OS Error","file":"src/core/lib/iomgr/resolve_address_windows.cc","file_line":96,"os_error":"No such host is known.\r\n","syscall":"getaddrinfo","wsa_error":11001}]}]}]}"
4

1 回答 1

2

在 Python gRPC 客户端中,需要调用不带协议 ( https:) 的通道。因此,我在 dotnet 核心框架中调用了 gRPC 服务,并且它起作用了。请注意,dotnet gRPC 服务器正在侦听https://localhost:5001.

with open('localhost.crt', 'rb') as f:
    credentials = grpc.ssl_channel_credentials(f.read())
with grpc.secure_channel('localhost:5001', credentials) as channel:
    stub = pb2_grpc.GreeterStub(channel)
    request = pb2.HelloRequest(name = "GreeterPythonClient")
    response = stub.SayHello(request)
于 2021-03-03T08:06:19.277 回答