Python struct module application. Convert data to binary stream

Source: Internet
Author: User
Tags unpack
Class Buffer:def __init__ (self, Bytes=none, recogn=10000): if (bytes = None): Self.length = 4 Self.buffer = Struct.pack (' i ', recogn) else:self.length = Len (bytes) Self.buffe
        r = Bytes Def write_char (self, c): Self.buffer = Self.buffer + struct.pack (' C ', c); Self.length = self.length + 1 def write_short (self, value): Self.buffer = Self.buffer + STRUCT.PA
        CK (' h ', value); Self.length = self.length + 2 def write_int (self, value): Self.buffer = Self.buffer + struct.pack (' I '
        , value); Self.length = self.length + 2 def write_utf (Self, Strings): Self.write_short (Len (strings)) SE
        Lf.buffer = Self.buffer + struct.pack ('%ds '% len (strings), strings); Self.length = Self.length + len (strings) def to_bytes (self): bytes = Struct.pack (' i ', Self.length + 4 ) + Self.buffer # a,b = struct.uNpack (' II ', bytes) return bytes def read_char (self): c, Self.buffer = Struct.upack (' c%ds '% (s Elf.buffer)-1), Self.buffer) return C def read_short (self): value, Self.buffer = Struct.unpack (  ' H%ds '% (len (self.buffer)-2), Self.buffer) return value def read_int (self): value, Self.buffer = Struct.unpack (' i%ds '% (len (self.buffer)-4), Self.buffer) return value def read_utf (self): l Ength = 0 Self.read_short (length) string, Self.buffer = Struct.unpack (' i%ds '% (len (self.buffer)-length) , Self.buffer) return string

Long ago, always wanted to write a game server, starting from the bottom reactor,socket,epoll. Finally, started two months ago, called DL Server engine. Write to now, the bottom network communication module basically finished work, has begun to divide it into the process of communication, such as certification services, gateways, logic engine and so on.

In testing reactor performance, lazy to write in C + +, decided to use Python to write a client test tool, Python is used to interview a game company to learn. Python's Twisted Network library works well, and it's decided to use Python plus twisted to do client testing. In the process of writing, it was found that Python did not convert various basic types into binary streams. Internet Baidu, found struct this module, very easy to use, like the C of printf format. If you want to know its detailed usage. Please Baidu.

Refer to the As for the network packet unpack and write package class, with Python wrote this buffer class, if it is the contract, with buffer (Recogn = 10000) This constructor, because the general contract to the server with a message number, RECOGN is the message number, After writing all kinds of data, call the To_bytes method to put the length of the package into a whole package. If the server sent over the network packet, with the buffer (bytes = package) This construction method, package is the server sent to the binary stream. Then call each read_* method.

Recently at work while busy working, while busy writing DL engine, feeling too busy, in the work out a lot of problems, come back at night has been thinking about how to improve the DL engine, write to two or three points. Feel a little tired. This time, temporary pen, the DL engine stop for a period of time, wait until there is free, then write, a good thing, need the best state to operate.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.