# -*- coding: utf-8 -*- """ Created on Tue Sep 2 19:26:15 2014 @author: agrigoryev """ from binascii import crc32 from datetime import datetime import io import json import socket import struct def vis(bs): """ Function to visualize byte streams. Split into bytes, print to console. :param bs: BYTE STRING """ symbols_in_one_line = 8 n = len(bs) // symbols_in_one_line for i in range(n): print(" ".join(["%02X" % b for b in bs[i*symbols_in_one_line:(i+1)*symbols_in_one_line]])) # for every 8 symbols line print(" ".join(["%02X" % b for b in bs[(i+1)*symbols_in_one_line:]])+"\n") # for last line class TlConstructor: def __init__(self, json_dict): self.id = int(json_dict['id']) self.type = json_dict['type'] self.predicate = json_dict['predicate'] self.params = [] # case of vector for param in json_dict['params']: if param['type'] == "Vector": param['type'] = "Vector t" param['subtype'] = "long" else: param['subtype'] = None self.params.append(param) class TlMethod: def __init__(self, json_dict): self.id = int(json_dict['id']) self.type = json_dict['type'] self.method = json_dict['method'] self.params = json_dict['params'] class TL: def __init__(self): with open("TL_schema.JSON", 'r') as f: TL_dict = json.load(f) # Read constructors self.constructors = TL_dict['constructors'] self.constructor_id = {} self.constructor_type = {} for elem in self.constructors: z = TlConstructor(elem) self.constructor_id[z.id] = z self.constructor_type[z.type] = z self.methods = TL_dict['methods'] self.method_id = {} self.method_name = {} for elem in self.methods: z = TlMethod(elem) self.method_id[z.id] = z self.method_name[z.method] = z def method_call(self, method, **kwargs): z = io.BytesIO() tl_method = self.method_name[method] z.write(struct.pack('>') vis(message) # Sending message visualisation to console data = self.header_unencrypted(message) + message step1 = struct.pack(' 0: # if we have smth. in the socket packet_length = struct.unpack("