美文网首页
Python protobuf 传输以及接收C#数据反序列化

Python protobuf 传输以及接收C#数据反序列化

作者: a9b854aded01 | 来源:发表于2019-06-20 11:26 被阅读0次

Protobuf(Google Protocol Buffers)是google开发的的一套用于数据存储,网络通信时用于协议编解码的工具库.它和XML和Json数据差不多,把数据已某种形式保存起来.Protobuf相对与XML和Json的不同之处,它是一种二进制的数据格式,具有更高的传输,打包和解包效率
基础教程请参考:https://blog.csdn.net/u013210620/article/details/81317731

protoType类型如下:
double、float、int32、int64、uint32、uint64、sint32、sint64、fixed32、fixed64、sfixed32、sfixed64、bool、string、bytes

编译环境windows安装:
【下载protoc】

https://github.com/google/protobuf/releases

根据自己的平台下载对应的编译器,我的是win10-64位:

image.png

解压到C:/Program Files下面:


image.png image.png

测试protoc:

新打开一个命令行:输入protoc --version,如果将输出版本号,说明protoc安装好了

image.png

详情参考:https://blog.csdn.net/menghaocheng/article/details/80176763

linux\mac 参考: https://blog.csdn.net/u013210620/article/details/81317731

要使用先生成一个protoc文件

image.png
syntax = "proto3";
package tutorial;

message Message{
    string Accepter = 1;
    bytes Protocal = 2;
    bytes Data = 3;
    string Sender = 4;
    int64 SendTick = 5;
    string LastID = 6;
}

生成protoc后使用命令生成.py文件 输出路径 本目录下

protoc .\Message.proto --python_out=./

另外protoc ./addressbook.proto –python_out=./这是输入的命令行指令,意思是在当前目录输出默认的即可。

image.png

使用方法:
序列化SerializeToString()
反序列化 ParseFromString()

from protobuff import Message_pb2
 image_file = Message_pb2.Message()
            image_file.Accepter = '111'
            test = image_file.SerializeToString()
            print(test)
image.png

下面说下与C#结合的例子:
C# 中使用的是 protobuf-net 2.4.0
实体类:

 [DataContract]
    [ProtoContract]
    public class Message : IMessage, IDisposable
    {
        /// <summary>
        ///     频道ID或私信ID
        /// </summary>
        [DataMember]
        [ProtoMember(1, IsRequired = false)]
        public string Accepter
        {
            get; set;
        }

        [DataMember]
        [ProtoMember(2, IsRequired = true)]
        public byte Protocal
        {
            get; set;
        }

        [DataMember]
        [ProtoMember(3, IsRequired = true)]
        public byte[] Data
        {
            get; set;
        }

        /// <summary>
        ///     发送者id
        /// </summary>
        [DataMember]
        [ProtoMember(4, IsRequired = true)]
        public string Sender
        {
            get; set;
        }

        /// <summary>
        ///     发送时间
        /// </summary>
        [DataMember]
        [ProtoMember(5, IsRequired = true)]
        public long SendTick
        {
            get; set;
        }

        [DataMember]
        [ProtoMember(6, IsRequired = true)]

        public string LastID
        {
            get; set;
        }

        public void Dispose()
        {
            this.Accepter = null;
            this.Protocal = 0;
            if (Data != null)
                Array.Clear(this.Data, 0, this.Data.Length);
            this.Sender = null;
            this.SendTick = 0;
        }
    }

C#客户端连接了Python websocket服务 并向其发送一条信息 内容:发送方,接收方,一张屏幕截图的流

Remote_Client_fubao.Model.Entity.Message msg = new Remote_Client_fubao.Model.Entity.Message
                        {
                            Accepter = "Python",
                            Sender = "C#",
                            Data = CompressHelper.Compress(ms.GetBuffer())

                        };
                        client._mCleint.ws.Send(SerializeHelper.ProtolBufSerialize(msg));

Python中接收到的信息:

image.png

我们将其反序列化

 image_file = Message_pb2.Message()
            image_file.Accepter = '111'
            test = image_file.SerializeToString()
            print(test)
            image_file.ParseFromString(bytes_data)

            print(image_file.Accepter)

结果:


image.png

希望能对还在困扰的同学一点启发 感谢收看_

相关文章

  • Python protobuf 传输以及接收C#数据反序列化

    Protobuf(Google Protocol Buffers)是google开发的的一套用于数据存储,网络通信...

  • 记录一次protobuf 段错误的bug

    此bug不具典型性,仅为个人开发笔记。 概况 在项目中使用了protobuf作为数据序列化以及消息体传输的协议。开...

  • 还在用json?让ProtocolBuffer为你的数据瘦瘦身

    前言 protobuf是什么的? Protocol Buffer是一种用于序列化数据的协议。可以用来通信传输,数据...

  • openGauss小坑#配置protobuf

    Protobuf是一种轻便高效的序列化数据结构的协议,用于网络通信和数据存储.体积小,序列化速度和传输速度快 op...

  • zookeeper 序列化协议

    [TOC]在网络传输时,传输的是二进制数据,所以发送端需要将序列化对象转变为二进制数据,也就是序列化过程。接收端需...

  • Dubbo源码分析(十一) 序列化机制

    下面我们来说一下Dubbo的序列化机制。数据在网络上进行传输,先要把数据序列化成字节流,接收端收到数据后,再反序列...

  • protobuf的坑

    项目中用到了Java的protobuf生成的序列化数据,在python中进行反序列化 版本为3.6.1 其中有一个...

  • zookeeper源码分析(5)-序列化和协议

    在网络传输时,传输的是二进制数据,所以发送端需要将序列化对象转变为二进制数据,也就是序列化过程。接收端需要将二进制...

  • Python的序列化与反序列化(pickle)

    序列化定义:将内存中的数据写入磁盘或者传输到网络中。 反序列化:将本地数据或者网络数据写入内存中。 Python ...

  • Hadoop序列化

    序列化 把内存中对象转换成字节序列(或其他数据传输协议)以便于存储到磁盘(持久化)和网络传输 反序列化 接收到的字...

网友评论

      本文标题:Python protobuf 传输以及接收C#数据反序列化

      本文链接:https://www.haomeiwen.com/subject/gqrdqctx.html