def _sendmessage(self, message, sock=None, trans_id=None, lock=None): message["v"] = get_version() if trans_id: message["t"] = trans_id encoded = bencode(message) if sock: if lock: with lock: sock.sendto(encoded, (self.host, self.port)) else: sock.sendto(encoded, (self.host, self.port))
def _perform_data_stitching(this): while len(this._buffer) > 0: try: temp = bcode.bdecode(this._buffer) except ValueError: temp = None if temp == None: return temp_len = len(bcode.bencode(temp)) this._buffer = this._buffer[temp_len:] map(lambda f: f(temp), this._cb)
def test_sends(self): logger = logging.getLogger("{0}:BcodeTransportUnitTest:test_sends".format(__name__)) def sendBytes(bs): logger.debug("sendBytes: {0}".format(bs)) sendBytes.received.append(bs) sendBytes.received = [] def receivedData(data): receivedData.data.append(data) receivedData.data = [] t = BCodeTransport(sendBytes, receivedData) t.send(4) t.send(["1", "2", "3", "4"]) self.assertEquals(0, len(receivedData.data)) self.assertEquals(bcode.bencode(4), sendBytes.received[0]) self.assertEquals(bcode.bencode(["1", "2", "3", "4"]), sendBytes.received[1])
def test_sends(self): logger = logging.getLogger( "{0}:BcodeTransportUnitTest:test_sends".format(__name__)) def sendBytes(bs): logger.debug("sendBytes: {0}".format(bs)) sendBytes.received.append(bs) sendBytes.received = [] def receivedData(data): receivedData.data.append(data) receivedData.data = [] t = BCodeTransport(sendBytes, receivedData) t.send(4) t.send(['1', '2', '3', '4']) self.assertEquals(0, len(receivedData.data)) self.assertEquals(bcode.bencode(4), sendBytes.received[0]) self.assertEquals(bcode.bencode(['1', '2', '3', '4']), sendBytes.received[1])
def send(self, data): '''sends the data encoded''' self._sender(bcode.bencode(data))
def test_encode_dict_in_dict(self): self.assertEqual(bcode.bencode({'fruit': {'a': 'apple', 'b': 'banana'}, 'cow': 'moo'}), 'd3:cow3:moo5:fruitd1:a5:apple1:b6:bananaee')
def test_encode_list_in_dict(self): self.assertEqual(bcode.bencode({'spam': ['a', 'b']}), 'd4:spaml1:a1:bee')
def test_encode_dict_key_order(self): """keys have to be sorted""" self.assertEqual(bcode.bencode({'a': 0, 'b': 1}), 'd1:ai0e1:bi1ee') self.assertEqual(bcode.bencode({'a': 0, 'b': 1, 'c': 2}), 'd1:ai0e1:bi1e1:ci2ee')
def test_encode_unicode_string_1(self): self.assertEqual(bcode.bencode(u'eggs'), '4:eggs')
def test_encode_xrange(self): self.assertEqual(bcode.bencode(xrange(1, 6)), 'li1ei2ei3ei4ei5ee')
def test_encode_tuple(self): self.assertEqual(bcode.bencode((1, 2)), 'li1ei2ee')
def test_encode_list(self): self.assertEqual(bcode.bencode(['spam', 'eggs']), 'l4:spam4:eggse')
def test_encode_float(self): self.assertEqual(bcode.bencode(3.3), '3:3.3')
def test_encode_integer(self): self.assertEqual(bcode.bencode(3), 'i3e')
def test_encode_unicode_string_2(self): self.assertEqual(bcode.bencode(u'pão'), '4:p\xc3\xa3o')
def encode(self, arg=None): return bcode.bencode(arg)
def send(self, data): """sends the data encoded""" self._sender(bcode.bencode(data))
def test_encode_dict_two_members(self): self.assertEqual(bcode.bencode({'cow': 'moo', 'spam': 'eggs'}), 'd3:cow3:moo4:spam4:eggse')
def test_encode_list_in_list(self): self.assertEqual(bcode.bencode(['spam', ['a', 'b']]), 'l4:spaml1:a1:bee')
def test_encode_string(self): self.assertEqual(bcode.bencode('spam'), '4:spam')