Python Post Protobuf

该博客介绍了如何利用protobuf的.proto文件定义数据结构,并通过protoc编译生成Python代码。在开发过程中,首先导入protobuf生成的Python模块,然后创建并填充AddressBook对象,将其序列化为字节串作为POST请求的payload。接着设置请求头,发送HTTP请求,最后从响应内容中反序列化获取数据。博客涵盖了protobuf的使用、HTTP请求和数据转换流程。
摘要由CSDN通过智能技术生成

背景

  1. 文中以protobuf的example提供的文件为例。
    // See README.txt for information and build instructions.
    //
    // Note: START and END tags are used in comments to define sections used in
    // tutorials.  They are not part of the syntax for Protocol Buffers.
    //
    // To get an in-depth walkthrough of this file and the related examples, see:
    // https://developers.google.com/protocol-buffers/docs/tutorials
    
    // [START declaration]
    syntax = "proto3";
    package tutorial;
    
    import "google/protobuf/timestamp.proto";
    // [END declaration]
    
    // [START java_declaration]
    option java_multiple_files = true;
    option java_package = "com.example.tutorial.protos";
    option java_outer_classname = "AddressBookProtos";
    // [END java_declaration]
    
    // [START csharp_declaration]
    option csharp_namespace = "Google.Protobuf.Examples.AddressBook";
    // [END csharp_declaration]
    
    // [START go_declaration]
    option go_package = "../tutorial";
    // [END go_declaration]
    
    // [START messages]
    message Person {
      string name = 1;
      int32 id = 2;  // Unique ID number for this person.
      string email = 3;
    
      enum PhoneType {
        MOBILE = 0;
        HOME = 1;
        WORK = 2;
      }
    
      message PhoneNumber {
        string number = 1;
        PhoneType type = 2;
      }
    
      repeated PhoneNumber phones = 4;
    
      google.protobuf.Timestamp last_updated = 5;
    }
    
    // Our address book file is just one of these.
    message AddressBook {
      repeated Person people = 1;
    }
    // [END messages]
    
  2. 根据.proto文件,生成Python用文件。(系统安装protobug之后,执行protoc --python_out=./ ./addressbook.proto,自动生成addressbook_pb2.py)

开发过程

  • 文件引入

正常包引入(import addressbook_pb2)。如果需要动态引入,可以将addressbook_pb2.py文件内容存在字符串中,使用__import__引入。

  • 生成post body数据
    addressbook= addressbook_pb2.AddressBook()
    persion = addressbook.Person

    persion.name = "test"
    persion.email = "test@test.com"

    payload=addressbook.SerializeToString()
  • 设置header
    headers = {
      'Content-Type': 'application/x-protobuf'
    }
  • 发送请求,反序列化结果
    response = requests.request("POST", url, headers=headers, data=payload)

    res_response = addressbook_pb2.AddressBook()

    res_response.ParseFromString(response.content)

    dict_list = []
    for r in res_response.Person:
        dict_list.append(MessageToDict(r))

    print(dict_list )

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Python中,可以使用protobufprotocol buffer)来实现数据的序列化和反序列化。首先,需要安装Protoc编译器,因为ProtobufPython上需要用C编译加速。然后,通过编写.proto文件描述数据结构,使用protoc命令将.proto文件编译成.py文件。生成的.py文件包含了生成和解析protobuf消息的代码。 Protobuf是谷歌Google开源的一个序列化框架,它可以将任意文件转换为序列化形式,并通过网络进行传输。由于其高效的字节编码和解码机制,Protobuf适合大数据进行传输的数据格式。在Python中使用Protobuf时,可以通过在.proto文件中定义消息类型和字段来指定数据结构,然后使用生成的.py文件中的代码来创建和操作protobuf消息对象。 例如,可以使用protobuf的SerializeToString()方法将protobuf消息对象序列化为字符串表示形式,以便进行网络传输或保存到文件中。同时,还可以使用反序列化方法将字符串解析为protobuf消息对象,以便在程序中进行操作和处理。 总结来说,在Python中使用protobuf,需要安装Protoc编译器并根据.proto文件生成.py文件。然后可以使用生成的代码来创建和操作protobuf消息对象,实现数据的序列化和反序列化。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【Protobuf】使用Python实现Protobuf数据框架](https://blog.csdn.net/qq_41682740/article/details/126571153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值