我对protobuf和所有这些都很陌生,但我正在尝试获取一个字典列表,并使用RPC/protobuf将它们写入服务。以下是原型:
syntax = "proto3";
package twittercontent.v1;
message TwitterContentRequest {
string messageId = 1;
bool isPrivate = 2;
string handleId = 3;
}
message TwitterContentResponse {
string response = 1;
}
service TwitterContentService {
rpc TwitterContent(TwitterContentRequest) returns (TwitterContentResponse) {}
}
我也有下面的DICT列表(这里只是测试数据):
test = [
{"messageId": "23452345324", "isPrivate": False, "handleId": "q35jmefn"},
{"messageId": "wegwer", "isPrivate": False, "handleId": "webwerbtetny"}
]
我不知道该怎么办,我试过这样的方法:
from twittercontentservice import twittercontent_pb2
def sendMsg(test):
result = []
for i in test:
unit = twittercontent_pb2.TwitterContentRequest()
unit.messageId = i['messageId']
unit.isPrivate = i['isPrivate']
unit.handleId = i['handleId']
result.append(unit)
return result
sendMsg(test)
但我认为这不起作用,当我打印函数的结果时,它只是test
列表中最后一个元素的列表。这里的任何指点都很好
Your proto is Wrong
根据您的原型,您在邮件中请求一个字典,并且需要多个字典。要解决这个问题,您需要添加一个repeated
关键字,这样您的proto就会变成这样:您的代码将
test
映射到twittercontent_pb2.TwitterContentRequest
列表中你可以用
print(len(sendMsg(test)))
来证明这一点,它应该返回2然而,这就是你所做的一切
您写下希望“将它们写入服务”,但您的问题包含的此服务的详细信息不足以提供答案
如果该服务是gRPC服务(这些服务使用protobuf消息),则您需要创建gRPC客户端并使用该客户端将您的消息提交到该服务
见:gRPCBasic Tutorial for Python
相关问题 更多 >
编程相关推荐