【问题标题】:Python gRPC server does not listen on specified portPython gRPC 服务器不监听指定端口
【发布时间】:2019-03-30 14:56:08
【问题描述】:

我正在尝试在this example 之后创建一个 gRPC python 服务器-客户端应用程序,但我无法将服务器放在我的代码上处于侦听状态。添加几乎与示例完全相同的代码并运行start 方法后,指定的端口上没有任何内容正在侦听。我的代码是:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import grpc
import interface_pb2
import interface_pb2_grpc
from concurrent import futures
import time
# some other imports...

class GrpcInterface(interface_pb2_grpc.ManipulaMapaServicer):
    def CriaItem(self, request, context):
        # do stuff...

    def LeItem(self, request, context):
        # do stuff...

    def AtualizaItem(self, request, context):
        # do stuff...

    def DeletaItem(self, request, context):
        # do stuff...

_ONE_DAY_IN_SECONDS = 60 * 60 * 24

def main():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    interface_pb2_grpc.add_ManipulaMapaServicer_to_server(GrpcInterface(), server)
    print('Vai iniciar o servidor gRPC na porta ' + str(8888))
    server.add_insecure_port('[::]:' + str(8888))
    server.start()
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop(0)

if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        print('Erro ao rodar servidor: ')
        print(str(e))

interface_pb2_grpc.ManipulaMapaServicer 的代码当然是根据我的interface.proto 自动生成的(使用命令python3 -m grpc_tools.protoc -I . --python_out=. --grpc_python_out=. interface.proto):

syntax = "proto3";

message msgItem {
    int64 chave = 1;
    string valor = 2;
}

message status {
    string resposta = 1; 
    msgItem itemResposta = 2; 
}

service ManipulaMapa {
    rpc CriaItem (msgItem) returns (status) {}
    rpc LeItem (msgItem) returns (msgItem) {}
    rpc AtualizaItem (msgItem) returns (status) {}
    rpc DeletaItem (msgItem) returns (status) {}
}

执行到达main 内的while True: 循环,但端口8888 上没有运行服务器。这里有什么问题?顺便说一句,这个问题与this 没有重复,因为在最后一个问题中,问题是由在start 方法之后运行的垃圾收集器引起的。

【问题讨论】:

  • 你在什么操作系统上运行这个?
  • 在 Devuan Linux 上
  • 我已经启动了一个虚拟机并试用了你的脚本,它正在运行。当然,这是一个基本的安装。也许,您可以尝试使用更高的端口号?你是如何尝试连接它的? nc?您是在同一台机器上尝试还是在另一台主机上尝试?
  • 我没有尝试连接它,因为它没有在听。如果我运行 netstat -tulpn 它不会出现在那里
  • 您在调用服务器时是否遇到任何错误?另外,您可以在调用服务器并共享回溯时尝试 strace 命令吗?干杯,Dheeraj

标签: python python-3.x protocol-buffers grpc grpc-python


【解决方案1】:

您可以在调用 main() 之前尝试启用日志记录。 如果 server.start() 调用过程中有任何错误,它会写入错误日志。

也就是在main()之前加上这个。

logging.basicConfig()

例如,如果端口已经被其他进程监听。

E0830 19:35:33.731000000 55600 src/core/ext/transport/chttp2/server/insecure/server_chttp2.cc:40] {"created":"@1567164933.731000000","description":"No address added out of total 1 resolved","file":"src/core/ext/transport/chttp2/server/chttp2_server.cc","file_line":394,"referenced_errors":[{"created":"@1567164933.731000000","description":"Failed to add port to server","file":"src/core/lib/iomgr/tcp_server_windows.cc","file_line":510,"referenced_errors":[{"created":"@1567164933.731000000","description":"OS Error","file":"src/core/lib/iomgr/tcp_server_windows.cc","file_line":201,"os_error":"Only one usage of each socket address (protocol/network address/port) is normally permitted.\r\n","syscall":"bind","wsa_error":10048}]}]}

【讨论】:

    猜你喜欢
    • 2022-08-18
    • 2011-09-14
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 2021-07-10
    • 1970-01-01
    相关资源
    最近更新 更多