Пример #1
0
        def on_got_metainfo(metainfo):
            if self.infohash:
                # Save the torrent to our store
                try:
                    self.session.save_collected_torrent(self.infohash, bencode(metainfo))
                except TypeError:
                    # TODO(Martijn): in libtorrent 1.1.1, bencode throws a TypeError which is a known bug
                    pass

            request.write(json.dumps({"metainfo": fix_unicode_dict(metainfo)}))
            self.finish_request(request)
Пример #2
0
    def write_data(self, message):
        """
        Write data over the event socket if it's open.
        """
        try:
            message_str = json.dumps(message)
        except UnicodeDecodeError:
            # The message contains invalid characters; fix them
            message_str = json.dumps(fix_unicode_dict(message))

        if len(self.events_requests) == 0:
            return
        else:
            [request.write(message_str + '\n') for request in self.events_requests]
Пример #3
0
 def test_fix_unicode_dict(self):
     """
     Testing the fix of a unicode dictionary
     """
     dict1 = {'a': 'b', 'c': 'd'}
     self.assertDictEqual(fix_unicode_dict(dict1), {'a': 'b', 'c': 'd'})
     dict2 = {'a': '\xa2'}
     self.assertDictEqual(fix_unicode_dict(dict2), {'a': ''})
     dict3 = {'a': [1, 2], 'b': ['1', '2']}
     self.assertDictEqual(fix_unicode_dict(dict3), {'a': [1, 2], 'b': ['1', '2']})
     dict4 = {'a': ['1', '2\xa3']}
     self.assertDictEqual(fix_unicode_dict(dict4), {'a': ['1', '2']})
     dict5 = {'a': ('1', '2\xa3')}
     self.assertDictEqual(fix_unicode_dict(dict5), {'a': ['1', '2']})
     dict6 = {'a': {'b': 'c\xa4'}}
     self.assertDictEqual(fix_unicode_dict(dict6), {'a': {'b': 'c'}})
     dict7 = {'a': 'ѡ'}
     self.assertDictEqual(fix_unicode_dict(dict7), {'a': u'ѡ'})
     obj = MockObject
     dict8 = {'a': {'b': obj}}
     self.assertDictEqual(fix_unicode_dict(dict8), {'a': {'b': obj}})
Пример #4
0
 def test_fix_unicode_dict(self):
     """
     Testing the fix of a unicode dictionary
     """
     dict1 = {'a': 'b', 'c': 'd'}
     self.assertDictEqual(fix_unicode_dict(dict1), {'a': 'b', 'c': 'd'})
     dict2 = {'a': '\xa2'}
     self.assertDictEqual(fix_unicode_dict(dict2), {'a': ''})
     dict3 = {'a': [1, 2], 'b': ['1', '2']}
     self.assertDictEqual(fix_unicode_dict(dict3), {'a': [1, 2], 'b': ['1', '2']})
     dict4 = {'a': ['1', '2\xa3']}
     self.assertDictEqual(fix_unicode_dict(dict4), {'a': ['1', '2']})
     dict5 = {'a': ('1', '2\xa3')}
     self.assertDictEqual(fix_unicode_dict(dict5), {'a': ['1', '2']})
     dict6 = {'a': {'b': 'c\xa4'}}
     self.assertDictEqual(fix_unicode_dict(dict6), {'a': {'b': 'c'}})
     dict7 = {'a': 'ѡ'}
     self.assertDictEqual(fix_unicode_dict(dict7), {'a': u'ѡ'})
     obj = MockObject
     dict8 = {'a': {'b': obj}}
     self.assertDictEqual(fix_unicode_dict(dict8), {'a': {'b': obj}})