def send_request(self, request): data = (request.format(), ) payload = zlib.compress(rencode.dumps(data)) self.conn.sendall(payload) buf = b'' while True: data = self.conn.recv(1024) if not data: self.connected = False break buf += data dobj = zlib.decompressobj() try: message = rencode.loads(dobj.decompress(buf)) except (ValueError, zlib.error, struct.error): # Probably incomplete data, read more continue else: buf = dobj.unused_data yield message
def data_received(self, data): self._buffer += data while self._buffer: decompressobj = zlib.decompressobj() try: data = decompressobj.decompress(self._buffer) except zlib.error: # Probably short read? return self._buffer = decompressobj.unused_data try: message = rencode.loads(data) except rencode.error: log().exception("bad data from server") continue if self.decode: message = _decode_recursive(message) log().debug("received: %s", message) try: self.message_received(message) except Exception: log().exception("Processing message: %s", message)
def get_data(self): self.request = loads(self.request) if not self.has_command(): return None, if self.request.has_key(self.DATA_KEY): self.srv_data = self.request.get(self.DATA_KEY) return self.srv_command, self.srv_data
def upload(obj, notification_format): data = crypt.encrypt_string_to_string(rencode.dumps(obj)) request = urllib2.urlopen(config.url, data=data) response_string = crypt.decrypt_stream_to_string(request) response = rencode.loads(response_string) url = response["url"] pyperclip.copy(url) subprocess.Popen(["notify-send", "--expire-time=2000", notification_format % url])
def found_terminator(self): data = loads(self._ibuffer) self._ibuffer = "" if type(dict()) == type(data) and data.has_key('action'): [getattr(self, n)(data) for n in ('Network', 'Network_' + data['action']) if hasattr(self, n)] else: print "OOB data (no such Network_action):", data
def found_terminator(self): data = loads(self._ibuffer) self._ibuffer = "" if type(dict()) == type(data) and data.has_key("action"): [getattr(self, n)(data) for n in ("Network", "Network_" + data["action"]) if hasattr(self, n)] else: print "OOB data (no such Network_action):", data
def upload(obj, notification_format): data = crypt.encrypt_string_to_string(rencode.dumps(obj)) request = urllib2.urlopen(config.url, data=data) response_string = crypt.decrypt_stream_to_string(request) response = rencode.loads(response_string) url = response["url"] pyperclip.copy(url) subprocess.Popen( ["notify-send", "--expire-time=2000", notification_format % url])
def found_terminator(self): #This is very tied-down to podsix's implementation so we'll need to keep it consistent as podsix updates. data = loads(self._ibuffer) self._ibuffer = "" if type(dict()) == type(data) and data.has_key('action'): self.NetworkAuth(data) else: print "OOB data:", data
def _handle_complete_message(self, data): """ Handles a complete message as it is transfered on the network. :param data: a zlib compressed string encoded with rencode. """ try: self.message_received(rencode.loads(zlib.decompress(data), decode_utf8=True)) except Exception as ex: log.warn('Failed to decompress (%d bytes) and load serialized data with rencode: %s', len(data), ex)
def _handle_complete_message(self, data): """ Handles a complete message as it is transfered on the network. :param data: a zlib compressed string encoded with rencode. """ try: self.message_received(rencode.loads(zlib.decompress(data))) except Exception, e: log.warn("Failed to decompress (%d bytes) and load serialized data "\ "with rencode: %s" % (len(data), str(e)))
def _handle_complete_message(self, data): """ Handles a complete message as it is transfered on the network. :param data: a zlib compressed string encoded with rencode. """ try: self.message_received( rencode.loads(zlib.decompress(data), decode_utf8=True)) except Exception, e: log.warn("Failed to decompress (%d bytes) and load serialized data "\ "with rencode: %s" % (len(data), str(e)))
def enable(self): self.core = component.get("Core") self.session = self.core.session self.torrents = self.core.torrentmanager.torrents self.pluginmanager = component.get("CorePluginManager") self.eventmanager = component.get("EventManager") self.alertmanager = component.get("AlertManager") self.alertmanager.register_handler("read_piece_alert", self.on_read_piece) self.alertmanager.register_handler("cache_flushed_alert", self.on_cache_flushed) self.eventmanager.register_event_handler("TorrentAddedEvent", self.on_torrent_add) self.eventmanager.register_event_handler("TorrentRemovedEvent", self.on_torrent_remove) self.pluginmanager.register_status_field("yatfsrpc.piece_bitfield", self.get_piece_bitfield) self.pluginmanager.register_status_field( "yatfsrpc.sequential_download", self.get_sequential_download) self.pluginmanager.register_status_field("yatfsrpc.piece_priority_map", self.get_piece_priority_map) self.pluginmanager.register_status_field( "yatfsrpc.keep_redundant_connections_map", self.get_keep_redundant_connections_map) self.pluginmanager.register_status_field("yatfsrpc.piece_priorities", self.get_piece_priorities) self.pluginmanager.register_status_field("yatfsrpc.cache_info", self.get_cache_info) state_path = os.path.join(deluge.configmanager.get_config_dir(), "yatfs.rencode") self.state_writer = StateWriter(state_path, name="yatfsrpc-state-writer") self.state_writer.daemon = True self.state_writer.start() if not hasattr(self, "torrent_to_piece_priority_maps"): try: with open(state_path, mode="rb") as f: self.torrent_to_piece_priority_maps = rencode.loads( f.read()) except: log.exception("While reading %s", state_path) self.torrent_to_piece_priority_maps = {} self.torrent_to_keep_redundant_connections_map = {} self.torrent_to_piece_to_data = {}
def _handle_complete_message(self, data): """ Handles a complete message as it is transfered on the network. :param data: a zlib compressed string encoded with rencode. """ try: message = rencode.loads(zlib.decompress(data), decode_utf8=True) except Exception as ex: log.warn("Failed to decompress (%d bytes) and load serialized data with rencode: %s", len(data), ex) return if type(message) is not tuple: log.error("Received invalid message: type is not tuple") return self.message_received(message)
def _handle_complete_message(self, data): """ Handles a complete message as it is transfered on the network. :param data: a zlib compressed string encoded with rencode. """ try: message = rencode.loads(zlib.decompress(data), decode_utf8=True) except Exception as ex: log.warn( "Failed to decompress (%d bytes) and load serialized data with rencode: %s", len(data), ex) return if type(message) is not tuple: log.error("Received invalid message: type is not tuple") return self.message_received(message)
def do_POST(self): data_str = crypt.decrypt_stream_to_string(self.rfile, limit=int(self.headers["Content-Length"])).strip() data = dict(rencode.loads(data_str)) data["time"] = time.time() while True: item_id = config.generate_name() if db.hlen(item_id) == 0: # todo: stop ignoring this race condition db.hmset(item_id, data) break self.send_response(200) self.end_headers() response = { "item": item_id, "url": config.url + "/" + item_id, } crypt.encrypt_string_to_stream(rencode.dumps(response), self.wfile)
def dataReceived(self, data): """ This method is called whenever data is received from a client. The only message that a client sends to the server is a RPC Request message. If the RPC Request message is valid, then the method is called in :meth:`dispatch`. :param data: the data from the client. It should be a zlib compressed rencoded string. :type data: str """ if self.__buffer: # We have some data from the last dataReceived() so lets prepend it data = self.__buffer + data self.__buffer = None while data: dobj = zlib.decompressobj() try: request = rencode.loads(dobj.decompress(data)) except Exception, e: #log.debug("Received possible invalid message (%r): %s", data, e) # This could be cut-off data, so we'll save this in the buffer # and try to prepend it on the next dataReceived() self.__buffer = data return else: data = dobj.unused_data if type(request) is not tuple: log.debug("Received invalid message: type is not tuple") return if len(request) < 1: log.debug("Received invalid message: there are no items") return for call in request: if len(call) != 4: log.debug( "Received invalid rpc request: number of items in request is %s", len(call)) continue #log.debug("RPCRequest: %s", format_request(call)) reactor.callLater(0, self.dispatch, *call)
def dataReceived(self, data): """ This method is called whenever data is received from a client. The only message that a client sends to the server is a RPC Request message. If the RPC Request message is valid, then the method is called in :meth:`dispatch`. :param data: the data from the client. It should be a zlib compressed rencoded string. :type data: str """ if self.__buffer: # We have some data from the last dataReceived() so lets prepend it data = self.__buffer + data self.__buffer = None while data: dobj = zlib.decompressobj() try: request = rencode.loads(dobj.decompress(data)) except Exception, e: #log.debug("Received possible invalid message (%r): %s", data, e) # This could be cut-off data, so we'll save this in the buffer # and try to prepend it on the next dataReceived() self.__buffer = data return else: data = dobj.unused_data if type(request) is not tuple: log.debug("Received invalid message: type is not tuple") return if len(request) < 1: log.debug("Received invalid message: there are no items") return for call in request: if len(call) != 4: log.debug("Received invalid rpc request: number of items " "in request is %s", len(call)) continue #log.debug("RPCRequest: %s", format_request(call)) reactor.callLater(0, self.dispatch, *call)
def start(): print "Starting WiiMote driver daemon" server = socketipc.IPCServer(port=31307) while True: connection = server.accept() try: while True: msg = rencode.loads(connection.receive()) cmd = msg[0] data = msg[1:] print 'command = ', cmd resp = respond(cmd, data) print 'response = ', resp connection.send(rencode.dumps(resp)) except RuntimeError, e: # They closed the connection, keep going pass
def addPartialMsg(self, msg, time=None): # add partial message to the buffer, return complete message and remove from buffer when all parts are received msgtype, identifier, nchunks, ichunk = msg[OAKN] nchunks = int(nchunks) ichunk = int(ichunk) ind = self.getPartialMsgIndex(identifier) if ind < 0: ind = self.crPartialMsg(identifier, nchunks) pmsg = self.incompleteRecvBuffer[ind] pmsg['data'][ichunk] = msg[DATA] pmsg['done'][ichunk] = True if time and msg[SEQN] < 0: pmsg['lastupdate'] = time # print pmsg['done'] if sum(pmsg['done']) == nchunks: # msg[DATA] = rencode.loads("".join(pmsg['data'])) msg[DATA] = rencode.loads("".join(pmsg['data'])) msg[MSG_TYPE] = msgtype self.incompleteRecvBuffer.remove(pmsg) return msg
def call(self, method, *args, **kwargs): self.request_id += 1 request = ((self.request_id, method, args, kwargs), ) comp_request = zlib.compress(rencode.dumps(request)) # send self._socket.send(comp_request) # recv data_queue = deque() while True: try: d = self._socket.recv(64) data_queue.append(d) except ssl.SSLError: raise try: data = zlib.decompress(b''.join(data_queue)) except zlib.error: if not d: raise continue break data = list(rencode.loads(data)) msg_type = data.pop(0) request_id = data.pop(0) print('request_id = {}'.format(request_id)) if msg_type == 2: exception_type, exception_msg, traceback = data[0] print(exception_type, exception_msg, traceback, sep=', ') raise Exception elif msg_type == 1: return data[0]
def send(self): if self.debug: logging.info( "Sending %s to %s " % ( len(self.data), str(self.address) ) ) ''' sending data to server ''' outgoing = dumps(self.data) if self.debug: logging.info( "Outgoing data: %s " % str(outgoing) ) self.socket.sendall(outgoing) ''' receiving data from server ''' self.received_data = self.socket.recv(self.BUFFER) self.received_data = loads(self.received_data) if not len(self.received_data): if self.debug: logging.error("No data received from server.") return if self.debug: logging.info( "Received data: %s " % str(self.received_data) )
def dataReceived(self, data): config = ConfigManager("gtkui.conf") data = rencode.loads(data) if not data or config["focus_main_window_on_add"]: component.get("MainWindow").present() process_args(data)
def unpack(data): '''unpacks data from socket ''' return loads(data)
def decode(self, data): # decode(and possibly decompress) the data with rencode return rencode.loads(data)
def unserialize(s): return rencode.loads(s)
def dataReceived(self, data): """ This method is called whenever we receive data from the daemon. :param data: a zlib compressed and rencoded string that should be either a RPCResponse, RCPError or RPCSignal """ if self.__buffer: # We have some data from the last dataReceived() so lets prepend it data = self.__buffer + data self.__buffer = None while data: # Increase the byte counter self.factory.bytes_recv += len(data) dobj = zlib.decompressobj() try: request = rencode.loads(dobj.decompress(data)) except Exception, e: #log.debug("Received possible invalid message (%r): %s", data, e) # This could be cut-off data, so we'll save this in the buffer # and try to prepend it on the next dataReceived() self.__buffer = data return else: data = dobj.unused_data if type(request) is not tuple: log.debug("Received invalid message: type is not tuple") return if len(request) < 3: log.debug( "Received invalid message: number of items in response is %s", len(3)) return message_type = request[0] if message_type == RPC_EVENT: event = request[1] #log.debug("Received RPCEvent: %s", event) # A RPCEvent was received from the daemon so run any handlers # associated with it. if event in self.factory.event_handlers: for handler in self.factory.event_handlers[event]: reactor.callLater(0, handler, *request[2]) continue request_id = request[1] # We get the Deferred object for this request_id to either run the # callbacks or the errbacks dependent on the response from the daemon. d = self.factory.daemon.pop_deferred(request_id) if message_type == RPC_RESPONSE: # Run the callbacks registered with this Deferred object d.callback(request[2]) elif message_type == RPC_ERROR: # Create the DelugeRPCError to pass to the errback r = self.__rpc_requests[request_id] e = DelugeRPCError(r.method, r.args, r.kwargs, request[2][0], request[2][1], request[2][2]) # Run the errbacks registered with this Deferred object d.errback(e) del self.__rpc_requests[request_id]
def dataReceived(self, data): """ This method is called whenever we receive data from the daemon. :param data: a zlib compressed and rencoded string that should be either a RPCResponse, RCPError or RPCSignal """ if self.__buffer: # We have some data from the last dataReceived() so lets prepend it data = self.__buffer + data self.__buffer = None while data: # Increase the byte counter self.factory.bytes_recv += len(data) dobj = zlib.decompressobj() try: request = rencode.loads(dobj.decompress(data)) except Exception, e: #log.debug("Received possible invalid message (%r): %s", data, e) # This could be cut-off data, so we'll save this in the buffer # and try to prepend it on the next dataReceived() self.__buffer = data return else: data = dobj.unused_data if type(request) is not tuple: log.debug("Received invalid message: type is not tuple") return if len(request) < 3: log.debug("Received invalid message: number of items in " "response is %s", len(3)) return message_type = request[0] if message_type == RPC_EVENT: event = request[1] #log.debug("Received RPCEvent: %s", event) # A RPCEvent was received from the daemon so run any handlers # associated with it. if event in self.factory.event_handlers: for handler in self.factory.event_handlers[event]: reactor.callLater(0, handler, *request[2]) continue request_id = request[1] # We get the Deferred object for this request_id to either run the # callbacks or the errbacks dependent on the response from the daemon. d = self.factory.daemon.pop_deferred(request_id) if message_type == RPC_RESPONSE: # Run the callbacks registered with this Deferred object d.callback(request[2]) elif message_type == RPC_ERROR: # Recreate exception and errback'it exception_cls = getattr(error, request[2]) exception = exception_cls(*request[3], **request[4]) # Ideally we would chain the deferreds instead of instance # checking just to log them. But, that would mean that any # errback on the fist deferred should returns it's failure # so it could pass back to the 2nd deferred on the chain. But, # that does not always happen. # So, just do some instance checking and just log rpc error at # diferent levels. r = self.__rpc_requests[request_id] msg = "RPCError Message Received!" msg += "\n" + "-" * 80 msg += "\n" + "RPCRequest: " + r.__repr__() msg += "\n" + "-" * 80 if isinstance(exception, error.WrappedException): msg += "\n" + exception.type + "\n" + exception.message + ": " msg += exception.traceback else: msg += "\n" + request[5] + "\n" + request[2] + ": " msg += str(exception) msg += "\n" + "-" * 80 if not isinstance(exception, error._ClientSideRecreateError): # Let's log these as errors log.error(msg) else: # The rest just get's logged in debug level, just to log # what's happening log.debug(msg) d.errback(exception) del self.__rpc_requests[request_id]
def data_received_old_protocol(self, data): """ This is the original method logic (as close as possible) for handling data receival on the client :param data: a zlib compressed string encoded with rencode. """ import zlib print('\n=== New Data Received ===\nBytes received:', len(data)) if self._buffer: # We have some data from the last dataReceived() so lets prepend it print('Current buffer:', len(self._buffer) if self._buffer else '0') data = self._buffer + data self._buffer = None self.packet_count += 1 self._bytes_received += len(data) while data: print('\n-- Handle packet data --') print('Bytes received:', self._bytes_received) print('Current data:', len(data)) if self._message_length == 0: # handle_new_message uses _buffer so set data to _buffer. self._buffer = data self._handle_new_message() data = self._buffer self._buffer = None self.packet_count = 1 print('New message of length:', self._message_length) dobj = zlib.decompressobj() try: request = rencode.loads(dobj.decompress(data)) print('Successfully loaded message', end=' ') print( ' - Buffer length: %d, data length: %d, unused length: %d' % ( len(data), len(data) - len(dobj.unused_data), len(dobj.unused_data), ) ) print('Packet count:', self.packet_count) except Exception as ex: # log.debug('Received possible invalid message (%r): %s', data, e) # This could be cut-off data, so we'll save this in the buffer # and try to prepend it on the next dataReceived() self._buffer = data print( 'Failed to load buffer (size %d): %s' % (len(self._buffer), str(ex)) ) return else: data = dobj.unused_data self._message_length = 0 self.message_received(request)
def dataReceived(self, data): # NOQA: N802 config = ConfigManager('gtkui.conf') data = rencode.loads(data, decode_utf8=True) if not data or config['focus_main_window_on_add']: component.get('MainWindow').present() process_args(data)
dataDict[0] = None dataDict[1] = self.points return dataDict def setState(self, weightOld, dataOld, weightNew, dataDictNew): self.char = NetEnt.entities[dataDictNew[0]] self.points = dataDictNew[1] NetEnt.registerSubclass(User) import rencode if __name__ == '__main__': #common chars = NetPool() #client state = {1:[2,3], 2:{'type':1, 0:('(empty)',[0.1,0.2,0.3]), 1:'charname'}} strstate = rencode.dumps(state) print len(strstate) print strstate state = rencode.loads(strstate) NetEnt.setState(state) #server #for i in range(10): # chars.add(Character()) print NetEnt.entities print NetEnt.types x = NetEnt.getState() print len(rencode.dumps(x)) print x
import rencode if __name__ == '__main__': #common chars = NetPool() #client state = { 1: [2, 3], 2: { 'type': 1, 0: ('(empty)', [0.1, 0.2, 0.3]), 1: 'charname' } } strstate = rencode.dumps(state) print len(strstate) print strstate state = rencode.loads(strstate) NetEnt.setState(state) #server #for i in range(10): # chars.add(Character()) print NetEnt.entities print NetEnt.types x = NetEnt.getState() print len(rencode.dumps(x)) print x
def _read_socket(socket): length, = struct.unpack("I", socket.recv(4)) return rencode.loads(socket.recv(length), decode_utf8=True)
def dataReceived(self, data): """ This method is called whenever we receive data from the daemon. :param data: a zlib compressed and rencoded string that should be either a RPCResponse, RCPError or RPCSignal """ if self.__buffer: # We have some data from the last dataReceived() so lets prepend it data = self.__buffer + data self.__buffer = None while data: # Increase the byte counter self.factory.bytes_recv += len(data) dobj = zlib.decompressobj() try: request = rencode.loads(dobj.decompress(data)) except Exception, e: #log.debug("Received possible invalid message (%r): %s", data, e) # This could be cut-off data, so we'll save this in the buffer # and try to prepend it on the next dataReceived() self.__buffer = data return else: data = dobj.unused_data if type(request) is not tuple: log.debug("Received invalid message: type is not tuple") return if len(request) < 3: log.debug( "Received invalid message: number of items in " "response is %s", len(3)) return message_type = request[0] if message_type == RPC_EVENT: event = request[1] #log.debug("Received RPCEvent: %s", event) # A RPCEvent was received from the daemon so run any handlers # associated with it. if event in self.factory.event_handlers: for handler in self.factory.event_handlers[event]: reactor.callLater(0, handler, *request[2]) continue request_id = request[1] # We get the Deferred object for this request_id to either run the # callbacks or the errbacks dependent on the response from the daemon. d = self.factory.daemon.pop_deferred(request_id) if message_type == RPC_RESPONSE: # Run the callbacks registered with this Deferred object d.callback(request[2]) elif message_type == RPC_ERROR: # Recreate exception and errback'it exception_cls = getattr(error, request[2]) exception = exception_cls(*request[3], **request[4]) # Ideally we would chain the deferreds instead of instance # checking just to log them. But, that would mean that any # errback on the fist deferred should returns it's failure # so it could pass back to the 2nd deferred on the chain. But, # that does not always happen. # So, just do some instance checking and just log rpc error at # diferent levels. r = self.__rpc_requests[request_id] msg = "RPCError Message Received!" msg += "\n" + "-" * 80 msg += "\n" + "RPCRequest: " + r.__repr__() msg += "\n" + "-" * 80 if isinstance(exception, error.WrappedException): msg += "\n" + exception.type + "\n" + exception.message + ": " msg += exception.traceback else: msg += "\n" + request[5] + "\n" + request[2] + ": " msg += str(exception) msg += "\n" + "-" * 80 if not isinstance(exception, error._ClientSideRecreateError): # Let's log these as errors log.error(msg) else: # The rest just get's logged in debug level, just to log # what's happening log.debug(msg) d.errback(exception) del self.__rpc_requests[request_id]
def handle_read(self): data ,addr = self.recvfrom(2048) if self.target: self.target.Network_UDP_data(loads(data))
def dataReceived(self, data): """ This method is called whenever we receive data from the daemon. :param data: a zlib compressed and rencoded string that should be either a RPCResponse, RCPError or RPCSignal """ if self.__buffer: # We have some data from the last dataReceived() so lets prepend it data = self.__buffer + data self.__buffer = None while data: # Increase the byte counter self.factory.bytes_recv += len(data) dobj = zlib.decompressobj() try: request = rencode.loads(dobj.decompress(data)) except Exception, e: #log.debug("Received possible invalid message (%r): %s", data, e) # This could be cut-off data, so we'll save this in the buffer # and try to prepend it on the next dataReceived() self.__buffer = data return else: data = dobj.unused_data if type(request) is not tuple: log.debug("Received invalid message: type is not tuple") return if len(request) < 3: log.debug("Received invalid message: number of items in response is %s", len(3)) return message_type = request[0] if message_type == RPC_EVENT: event = request[1] #log.debug("Received RPCEvent: %s", event) # A RPCEvent was received from the daemon so run any handlers # associated with it. if event in self.factory.event_handlers: for handler in self.factory.event_handlers[event]: reactor.callLater(0, handler, *request[2]) continue request_id = request[1] # We get the Deferred object for this request_id to either run the # callbacks or the errbacks dependent on the response from the daemon. d = self.factory.daemon.pop_deferred(request_id) if message_type == RPC_RESPONSE: # Run the callbacks registered with this Deferred object d.callback(request[2]) elif message_type == RPC_ERROR: # Create the DelugeRPCError to pass to the errback r = self.__rpc_requests[request_id] e = DelugeRPCError(r.method, r.args, r.kwargs, request[2][0], request[2][1], request[2][2]) # Run the errbacks registered with this Deferred object d.errback(e) del self.__rpc_requests[request_id]
def decode(self, data): # decode the data with rencode # return rencode.loads(data) return rencode.loads(data)
def decode_msg(self, msg): return Msg(*rencode.loads(msg))