Exemplo n.º 1
0
Arquivo: node.py Projeto: wycstar/dht
 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 _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)
Exemplo n.º 4
0
    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])
Exemplo n.º 5
0
    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])
Exemplo n.º 6
0
    def send(self, data):
        '''sends the data encoded'''

        self._sender(bcode.bencode(data))
Exemplo n.º 7
0
 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')
Exemplo n.º 8
0
 def test_encode_list_in_dict(self):
     self.assertEqual(bcode.bencode({'spam': ['a', 'b']}), 'd4:spaml1:a1:bee')
Exemplo n.º 9
0
 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')
Exemplo n.º 10
0
 def test_encode_unicode_string_1(self):
     self.assertEqual(bcode.bencode(u'eggs'), '4:eggs')
Exemplo n.º 11
0
 def test_encode_xrange(self):
     self.assertEqual(bcode.bencode(xrange(1, 6)), 'li1ei2ei3ei4ei5ee')
Exemplo n.º 12
0
 def test_encode_tuple(self):
     self.assertEqual(bcode.bencode((1, 2)), 'li1ei2ee')
Exemplo n.º 13
0
 def test_encode_list(self):
     self.assertEqual(bcode.bencode(['spam', 'eggs']), 'l4:spam4:eggse')
Exemplo n.º 14
0
 def test_encode_float(self):
     self.assertEqual(bcode.bencode(3.3), '3:3.3')
Exemplo n.º 15
0
 def test_encode_integer(self):
     self.assertEqual(bcode.bencode(3), 'i3e')
Exemplo n.º 16
0
 def test_encode_unicode_string_2(self):
     self.assertEqual(bcode.bencode(u'pão'), '4:p\xc3\xa3o')
Exemplo n.º 17
0
 def encode(self, arg=None):
     return bcode.bencode(arg)
Exemplo n.º 18
0
    def send(self, data):
        """sends the data encoded"""

        self._sender(bcode.bencode(data))
Exemplo n.º 19
0
 def test_encode_dict_two_members(self):
     self.assertEqual(bcode.bencode({'cow': 'moo', 'spam': 'eggs'}),
                      'd3:cow3:moo4:spam4:eggse')
Exemplo n.º 20
0
 def test_encode_list_in_list(self):
     self.assertEqual(bcode.bencode(['spam', ['a', 'b']]), 'l4:spaml1:a1:bee')
Exemplo n.º 21
0
 def test_encode_string(self):
     self.assertEqual(bcode.bencode('spam'), '4:spam')