ReadInt (), ReadByte (), ReadString (), etc. In python?

Functions ReadInt (), ReadByte () and ReadString () (to name a few) exist in other languages ​​for reading input from streams. I am trying to read from a socket and I want to use such functions. Are they hidden somewhere else in Python or did someone create a library for it?

In addition, there are copies of Writedatatype ().

+4
source share
4 answers

I think struct.unpack_from is what you are looking for.

+8
source

The Python path uses struct.unpack to read binary data. I am very used to BinaryReader and BinaryWriter in C #, so I did this:

from struct import * class BinaryStream: def __init__(self, base_stream): self.base_stream = base_stream def readByte(self): return self.base_stream.read(1) def readBytes(self, length): return self.base_stream.read(length) def readChar(self): return self.unpack('b') def readUChar(self): return self.unpack('B') def readBool(self): return self.unpack('?') def readInt16(self): return self.unpack('h', 2) def readUInt16(self): return self.unpack('H', 2) def readInt32(self): return self.unpack('i', 4) def readUInt32(self): return self.unpack('I', 4) def readInt64(self): return self.unpack('q', 8) def readUInt64(self): return self.unpack('Q', 8) def readFloat(self): return self.unpack('f', 4) def readDouble(self): return self.unpack('d', 8) def readString(self): length = self.readUInt16() return self.unpack(str(length) + 's', length) def writeBytes(self, value): self.base_stream.write(value) def writeChar(self, value): self.pack('c', value) def writeUChar(self, value): self.pack('C', value) def writeBool(self, value): self.pack('?', value) def writeInt16(self, value): self.pack('h', value) def writeUInt16(self, value): self.pack('H', value) def writeInt32(self, value): self.pack('i', value) def writeUInt32(self, value): self.pack('I', value) def writeInt64(self, value): self.pack('q', value) def writeUInt64(self, value): self.pack('Q', value) def writeFloat(self, value): self.pack('f', value) def writeDouble(self, value): self.pack('d', value) def writeString(self, value): length = len(value) self.writeUInt16(length) self.pack(str(length) + 's', value) def pack(self, fmt, data): return self.writeBytes(pack(fmt, data)) def unpack(self, fmt, length = 1): return unpack(fmt, self.readBytes(length))[0] 

Once you have the stream, you put it in the BinaryStream constructor, and you have the BinaryStream :)

Example:

 from binary import BinaryStream f = open("Users", "rb") stream = BinaryStream(f) users_count = stream.readUInt64() for i in range(users_count): username = stream.readString() password = stream.readString() 
+10
source

I used the Zippoxer code and it works well for everything, thanks.

However, I had some problem with readString (). A C # document indicated that the length is encoded by 7 bytes. So I used readUhar instead of readUInt16:

 def readString(self): length = self.readUChar() return self.unpack(str(length) + 's', length) 

and now it works. Maybe this is characteristic of my problem? But it may help someone ...

+1
source

Note: Python does not have readByte, readInt, and readString because it does not work directly with all these fancy data types. Files provide strings that you can convert.

Python <= 2.6 has String and what you get from your input streams is strings. A simple socket.read() provides this input. You can use struct to convert a stream to a sequence of integers. The important thing is that packet conversion and decompression can be a byte, a word, long, or any other, but the result of Python is an integer.

Thus, your input may be bytes, but Python presents it as a string, most of which is not printable. Your desire can be an array of individual values, each of which ranges from 0 to 255, which are numeric versions of these bytes. Python represents them as integers.

Python >= 3.0 has bytearrays that can be used to process bytes directly. You can convert them to strings or integers (including bytes and longs) or something else.

0
source

All Articles