Created
May 19, 2016 11:40
-
-
Save TerrorBite/89df0d97ebba2dbb3f58d8e5f8f11ce1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generated by the protocol buffer compiler. DO NOT EDIT! | |
# source: chat.proto | |
import sys | |
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1')) | |
from google.protobuf import descriptor as _descriptor | |
from google.protobuf import message as _message | |
from google.protobuf import reflection as _reflection | |
from google.protobuf import symbol_database as _symbol_database | |
from google.protobuf import descriptor_pb2 | |
# @@protoc_insertion_point(imports) | |
_sym_db = _symbol_database.Default() | |
DESCRIPTOR = _descriptor.FileDescriptor( | |
name='chat.proto', | |
package='', | |
syntax='proto3', | |
serialized_pb=_b('\n\nchat.proto\".\n\x0b\x43hatMessage\x12\x11\n\ttimestamp\x18\x01 \x01(\x05\x12\x0c\n\x04text\x18\x02 \x01(\tb\x06proto3') | |
) | |
_sym_db.RegisterFileDescriptor(DESCRIPTOR) | |
_CHATMESSAGE = _descriptor.Descriptor( | |
name='ChatMessage', | |
full_name='ChatMessage', | |
filename=None, | |
file=DESCRIPTOR, | |
containing_type=None, | |
fields=[ | |
_descriptor.FieldDescriptor( | |
name='timestamp', full_name='ChatMessage.timestamp', index=0, | |
number=1, type=5, cpp_type=1, label=1, | |
has_default_value=False, default_value=0, | |
message_type=None, enum_type=None, containing_type=None, | |
is_extension=False, extension_scope=None, | |
options=None), | |
_descriptor.FieldDescriptor( | |
name='text', full_name='ChatMessage.text', index=1, | |
number=2, type=9, cpp_type=9, label=1, | |
has_default_value=False, default_value=_b("").decode('utf-8'), | |
message_type=None, enum_type=None, containing_type=None, | |
is_extension=False, extension_scope=None, | |
options=None), | |
], | |
extensions=[ | |
], | |
nested_types=[], | |
enum_types=[ | |
], | |
options=None, | |
is_extendable=False, | |
syntax='proto3', | |
extension_ranges=[], | |
oneofs=[ | |
], | |
serialized_start=14, | |
serialized_end=60, | |
) | |
DESCRIPTOR.message_types_by_name['ChatMessage'] = _CHATMESSAGE | |
ChatMessage = _reflection.GeneratedProtocolMessageType('ChatMessage', (_message.Message,), dict( | |
DESCRIPTOR = _CHATMESSAGE, | |
__module__ = 'chat_pb2' | |
# @@protoc_insertion_point(class_scope:ChatMessage) | |
)) | |
_sym_db.RegisterMessage(ChatMessage) | |
# @@protoc_insertion_point(module_scope) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from chat_pb2 import ChatMessage | |
import time | |
import struct | |
def main(): | |
print repr(pack_message("Hello!")) | |
def pack_message(text): | |
# create protocol buffer and insert data | |
msg = ChatMessage() | |
msg.timestamp = int(time.time()) | |
msg.text = text | |
# serialize it | |
data = msg.SerializeToString() | |
header = struct.pack('!H', len(data)) # length as two-byte unsigned short in network byte order | |
return header+data | |
if __name__ == '__main__': main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment