Tam*_*mpa 0 python protocol-buffers
我试图在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;
}
Run Code Online (Sandbox Code Playgroud)
现在,下面是我试图提交的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
Run Code Online (Sandbox Code Playgroud)
当我使用以下提交时:
'Content-Type', 'application/octet-stream'
bid_response.SerializeToString()
Run Code Online (Sandbox Code Playgroud)
返回的所有内容都是bid_response.processing_time_ms.
我怀疑我没有正确地重复和消息.
我记得有一段时间在我的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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13745 次 |
| 最近记录: |