I want to get a Grpc demo which client is Golang and server is Python. I use Grpc official code as my demo, but it's doesn't work. Can someone help me?
If I generate the client and server by same language, example: Python or Golang. It works correctly.
- helloworld.proto
syntax = "proto3";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
2.get relevant code
1) get python relevant code: python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. helloworld.proto
2) get go relevant code: protoc -I . helloworld.proto --go_out=plugins=grpc:.
- helloworld_grpc_server.py
import time
import logging
import grpc
from concurrent import futures
import helloworld_pb2
import helloworld_pb2_grpc
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
class Greeter(helloworld_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port('[::]:50051')
server.start()
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__':
logging.basicConfig()
serve()
- greeter_client/main.go
package main
import (
"context"
"log"
"os"
"time"
"google.golang.org/grpc"
pb "demo/grpc/go_python/helloworld"
)
const (
address = "localhost:50051"
defaultName = "world"
)
func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.Message)
}
- run the demo
server:
python helloworld_grpc_server.py
client:
go run main.go
But, I get the wrong output for client:
2019/08/07 19:18:03 could not greet: rpc error: code = Unimplemented desc = Method not found!
exit status 1
I expect to the demo work.