my goal is to communicate with my gRPC server (enabled reflection) with client that is not familiar of server proto file, for that i need to find the server using port number only.
im trying to find my gRPC server using socket.getservbyport and get "port/proto not found" exception. i know my server is up & running on this port. what am i missing here ?
server side:
from concurrent import futures
import logging
import grpc
from grpc_reflection.v1alpha import reflection
import helloworld_pb2
import helloworld_pb2_grpc
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)
SERVICE_NAMES = (
helloworld_pb2.DESCRIPTOR.services_by_name['Greeter'].full_name,
reflection.SERVICE_NAME,
)
reflection.enable_server_reflection(SERVICE_NAMES, server)
server.add_insecure_port('[::]:50051')
server.start()
server.wait_for_termination()
client side:
import socket
def find_service_name():
for port in [25, 80, 50051]:
print("Port: %s => service name: %s" % (port, socket.getservbyport(port)))
def run():
try:
find_service_name()
except Exception as e:
print(e)
with grpc.insecure_channel('localhost:50051') as channel:
stub = helloworld_pb2_grpc.GreeterStub(channel)
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
print(response)
if __name__ == '__main__':
logging.basicConfig()
run()
output:
Port: 25 => service name: smtp
Port: 80 => service name: http
port/proto not found
message: "Hello, you!"