我正在尝试使用python中的协议缓冲区提交响应。以下是结构。
message BidResponse {
message Ad {
optional string html_snippet = 1;
message TemplateParameter {
optional string parameter_value = 1;
optional string blank_ad_parameter_value = 8;
optional string buyer_creative_id = 2;
optional string click_through_url = 3;
optional int32 left = 4;
optional int32 right = 5;
optional int32 top = 6;
optional int32 bottom = 7;
optional int32 backup_index = 9;
};
repeated TemplateParameter template_parameter = 13;
repeated string click_through_url = 4;
repeated int32 vendor_type = 5;
message AdSlot {
required int32 id = 1;
required int64 max_cpm_micros = 2;
}
repeated AdSlot adslot = 3;
}
repeated Ad ad = 2;
optional int32 processing_time_ms = 4;
}
现在,下面是我试图提交的python代码。
ms = (time.time() - start)*1000
bid_response = realtime_bidding_pb2.BidResponse()
bid_response.processing_time_ms = int(ms)
ad = bid_response.Ad()
ad.html_snippet = """<img src='http://cdn.test.com/test.gif' />"""
ad.click_through_url = """test.com"""
adslot = ad.AdSlot()
adslot.id = adslots_id[0]
adslot.max_cpm_micros=150000000
当我使用以下内容提交时:
'Content-Type', 'application/octet-stream'
bid_response.SerializeToString()
返回的所有内容都是bid_response.processing_time_ms。
我怀疑我没有正确地重复和消息。
答案 0 :(得分:6)
我记得有一段时间在我的python代码中花了一段时间来搜索它。你可以在这里找到python中字段界面的文档:http://code.google.com/apis/protocolbuffers/docs/reference/python-generated.html#fields。
你想要的是add
成员函数 - 使用你的代码看起来像这样:
ms = (time.time() - start)*1000
bid_response = realtime_bidding_pb2.BidResponse()
bid_response.processing_time_ms = int(ms)
ad = bid_response.ad.add()
ad.html_snippet = """<img src='http://cdn.test.com/test.gif' />"""
ad.click_through_url = """test.com"""
adslot = ad.adslot.add()
adslot.id = adslots_id[0]
adslot.max_cpm_micros=150000000
答案 1 :(得分:0)
您是否阅读过the documentation?这很清楚。
你应该替换
ad = bid_response.Ad()
与
ad = bid_response.ad.add()
同样适用于AdSlot。