0

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!"

1 Answers1

0

Python's getservbyport is a wrapper around a system call of the same name (see the relevant source code for cpython).

The system call (documentation here) looks at the services database:

The getservbyport() function returns a servent structure for the entry from the database that matches the port port ...

The services database that getservbyport goes to is just (quoting):

a plain ASCII file providing a mapping between human-friendly textual names for internet services, and their underlying assigned port numbers and protocol types.

So, what you're seeing is that your server doesn't appear in the services database. It's not surprising, because AFAIK nothing added your service to the database.

Roy2012
  • 11,755
  • 2
  • 22
  • 35