Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

ReadInt() ReadByte() ReadString() etc in Python

By: a guest on Feb 27th, 2012  |  syntax: None  |  size: 2.30 KB  |  views: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from struct import *
  2.  
  3. class BinaryStream:
  4.     def __init__(self, base_stream):
  5.         self.base_stream = base_stream
  6.  
  7.     def readByte(self):
  8.         return self.base_stream.read(1)
  9.  
  10.     def readBytes(self, length):
  11.         return self.base_stream.read(length)
  12.  
  13.     def readChar(self):
  14.         return self.unpack('b')
  15.  
  16.     def readUChar(self):
  17.         return self.unpack('B')
  18.  
  19.     def readBool(self):
  20.         return self.unpack('?')
  21.  
  22.     def readInt16(self):
  23.         return self.unpack('h', 2)
  24.  
  25.     def readUInt16(self):
  26.         return self.unpack('H', 2)
  27.  
  28.     def readInt32(self):
  29.         return self.unpack('i', 4)
  30.  
  31.     def readUInt32(self):
  32.         return self.unpack('I', 4)
  33.  
  34.     def readInt64(self):
  35.         return self.unpack('q', 8)
  36.  
  37.     def readUInt64(self):
  38.         return self.unpack('Q', 8)
  39.  
  40.     def readFloat(self):
  41.         return self.unpack('f', 4)
  42.  
  43.     def readDouble(self):
  44.         return self.unpack('d', 8)
  45.  
  46.     def readString(self):
  47.         length = self.readUInt16()
  48.         return self.unpack(str(length) + 's', length)
  49.  
  50.     def writeBytes(self, value):
  51.         self.base_stream.write(value)
  52.  
  53.     def writeChar(self, value):
  54.         self.pack('c', value)
  55.  
  56.     def writeUChar(self, value):
  57.         self.pack('C', value)
  58.  
  59.     def writeBool(self, value):
  60.         self.pack('?', value)
  61.  
  62.     def writeInt16(self, value):
  63.         self.pack('h', value)
  64.  
  65.     def writeUInt16(self, value):
  66.         self.pack('H', value)
  67.  
  68.     def writeInt32(self, value):
  69.         self.pack('i', value)
  70.  
  71.     def writeUInt32(self, value):
  72.         self.pack('I', value)
  73.  
  74.     def writeInt64(self, value):
  75.         self.pack('q', value)
  76.  
  77.     def writeUInt64(self, value):
  78.         self.pack('Q', value)
  79.  
  80.     def writeFloat(self, value):
  81.         self.pack('f', value)
  82.  
  83.     def writeDouble(self, value):
  84.         self.pack('d', value)
  85.  
  86.     def writeString(self, value):
  87.         length = len(value)
  88.         self.writeUInt16(length)
  89.         self.pack(str(length) + 's', value)
  90.  
  91.     def pack(self, fmt, data):
  92.         return self.writeBytes(pack(fmt, data))
  93.  
  94.     def unpack(self, fmt, length = 1):
  95.         return unpack(fmt, self.readBytes(length))[0]
  96.        
  97. from binary import BinaryStream
  98.  
  99. f = open("Users", "rb")
  100. stream = BinaryStream(f)
  101. users_count = stream.readUInt64()
  102. for i in range(users_count):
  103.     username = stream.readString()
  104.     password = stream.readString()