grpc/grpc-dotnet

How to communicate the GRPC server with Python client

dwang-git opened this issue · 2 comments

I'm trying to create a client with Python to communicate with the example GreeterService. Here is how I set up the client with Python:

  1. Use the command to export ASP.Net development certificate as suggested in this link:
    dotnet dev-certs https --export-path aspnet_cert.cert
  2. Generate the greet_pb2_grpc.py and greet_pb2.py using the command:
    python -m grpc_tools.protoc -I=. --python_out=. --pyi_out=. --grpc_python_out=. greet.proto
  3. Create the greeter_client.py as below:
     import grpc
     import greet_pb2
     import greet_pb2_grpc
     from os import environ
     environ["GRPC_DNS_RESOLVER"] = "native"
    
     def run_command():
        with open('aspnet_cert.cert', 'rb') as f:
            credentials = grpc.ssl_channel_credentials(f.read())
            # channel = grpc.secure_channel('dns:localhost:5001', credentials)
            # channel = grpc.secure_channel('localhost:5001', credentials)
            # channel = grpc.secure_channel('{ip}:50051', credentials)
            channel = grpc.secure_channel('https://localhost:5001', credentials)
            stub = greet_pb2_grpc.GreeterStub(channel)
            request = greet_pb2.HelloRequest(name = "Python_GreeterClient" )
            response = stub.SayHello(request)
            print(response.message)
    
    if __name__ == '__main__':
        run_command()
    

But when I run greeter_client.py, I would always get the error below. 


> <_InactiveRpcError of RPC that terminated with:
> 	status = StatusCode.UNAVAILABLE
> 	details = "DNS resolution failed for https://localhost:5001: UNAVAILABLE: getaddrinfo: WSA Error (No such host is known.
>  -- 11001)"
> 	debug_error_string = "UNKNOWN:Error received from peer  {grpc_message:"DNS resolution failed for https://localhost:5001: UNAVAILABLE: getaddrinfo: WSA Error (No such host is known.\r\n -- 11001)", grpc_status:14, created_time:"2024-12-09T19:52:27.9184037+00:00"}"
> >
>   File "C:\Cortina\Python_cableeye_wrapper\greeter_client.py", line 16, in run_command
>     response = stub.SayHello(request)
>   File "C:\Cortina\Python_cableeye_wrapper\greeter_client.py", line 20, in <module>
>     run_command()

and in the console output, I see this:

> E0000 00:00:1733783545.954314   13220 ssl_transport_security.cc:755] Could not load any root certificate.     
> E0000 00:00:1733783545.956913   13220 ssl_transport_security.cc:2302] Cannot load server root certificates.   
> E0000 00:00:1733783545.957548   13220 ssl_credentials.cc:215] Handshaker factory creation failed with TSI_INVALID_ARGUMENT

Seems the certificate was not generated correctly?

I tried some other ways of creating the channel, but none of them works so far.
Thanks!

Please ask questions about configuring a python client in the python gRPC repository. I don't know how it works.

Can you point out the link of the python gRPC repository? Thanks!