def test_equal(self) -> None: data = lib.get_random_bytes(20) sha1_1 = lt.sha1_hash(data) sha1_2 = lt.sha1_hash(data) self.assertEqual(sha1_1, sha1_2) self.assertEqual(hash(sha1_1), hash(sha1_2))
def test_attributes(self) -> None: fe = lt.file_entry() fe.path = os.path.join("path", "file.txt") path = fe.path self.assertEqual(path, os.path.join("path", "file.txt")) fe.symlink_path = os.path.join("path", "other.txt") self.assertEqual(fe.symlink_path, os.path.join("path", "other.txt")) fe.filehash = lt.sha1_hash(b"a" * 20) self.assertEqual(fe.filehash, lt.sha1_hash(b"a" * 20)) fe.mtime = 123456 self.assertEqual(fe.mtime, 123456) with self.assertWarns(DeprecationWarning): self.assertFalse(fe.pad_file) with self.assertWarns(DeprecationWarning): self.assertFalse(fe.executable_attribute) with self.assertWarns(DeprecationWarning): self.assertFalse(fe.hidden_attribute) with self.assertWarns(DeprecationWarning): self.assertFalse(fe.symlink_attribute) with self.assertWarns(DeprecationWarning): self.assertEqual(fe.offset, 0) with self.assertWarns(DeprecationWarning): self.assertEqual(fe.size, 0)
def test_add_magnet_link(self): ses = lt.session() atp = lt.add_torrent_params() atp.info_hash = lt.sha1_hash(b"a" * 20) h = ses.add_torrent(atp) self.assertTrue( h.status().info_hashes == lt.info_hash_t(lt.sha1_hash(b"a" * 20)))
def test_torrent_info_sha1_overload(self): ti = lt.torrent_info(lt.info_hash_t(lt.sha1_hash(b'a' * 20))) self.assertEqual(ti.info_hash(), lt.sha1_hash(b'a' * 20)) self.assertEqual(ti.info_hashes().v1, lt.sha1_hash(b'a' * 20)) ti_copy = lt.torrent_info(ti) self.assertEqual(ti_copy.info_hash(), lt.sha1_hash(b'a' * 20)) self.assertEqual(ti_copy.info_hashes().v1, lt.sha1_hash(b'a' * 20))
def test_sha1_constructor(self): if not HAVE_DEPRECATED_APIS: return info = lt.torrent_info(lt.sha1_hash('aaaaaaaaaaaaaaaaaaaa')) self.assertEqual(info.info_hash(), lt.sha1_hash('aaaaaaaaaaaaaaaaaaaa')) self.assertEqual(info.info_hashes().v1, lt.sha1_hash('aaaaaaaaaaaaaaaaaaaa'))
def test_find_torrent(self): s = lt.session(settings) h = s.add_torrent({'info_hash': b"a" * 20, 'save_path': '.'}) self.assertTrue(h.is_valid()) h2 = s.find_torrent(lt.sha1_hash(b"a" * 20)) self.assertTrue(h2.is_valid()) h3 = s.find_torrent(lt.sha1_hash(b"b" * 20)) self.assertFalse(h3.is_valid()) self.assertEqual(h, h2) self.assertNotEqual(h, h3)
def test_info_hash(self): s1 = lt.sha1_hash(b'a' * 20) s2 = lt.sha256_hash(b'b' * 32) ih1 = lt.info_hash_t(s1) self.assertTrue(ih1.has_v1()) self.assertFalse(ih1.has_v2()) self.assertEqual(ih1.v1, s1) ih2 = lt.info_hash_t(s2) self.assertFalse(ih2.has_v1()) self.assertTrue(ih2.has_v2()) self.assertEqual(ih2.v2, s2) ih12 = lt.info_hash_t(s1, s2) self.assertTrue(ih12.has_v1()) self.assertTrue(ih12.has_v2()) self.assertEqual(ih12.v1, s1) self.assertEqual(ih12.v2, s2) self.assertNotEqual(hash(ih1), hash(ih2)) self.assertNotEqual(hash(ih1), hash(ih12)) self.assertEqual(hash(ih1), hash(lt.info_hash_t(s1))) self.assertEqual(hash(ih2), hash(lt.info_hash_t(s2))) self.assertEqual(hash(ih12), hash(lt.info_hash_t(s1, s2)))
def test_read_resume_data(self): resume_data = lt.bencode({'file-format': 'libtorrent resume file', 'info-hash': 'abababababababababab', 'name': 'test', 'save_path': '.', 'peers': '\x01\x01\x01\x01\x00\x01\x02\x02\x02\x02\x00\x02', 'file_priority': [0, 1, 1]}) tp = lt.read_resume_data(resume_data) self.assertEqual(tp.name, 'test') self.assertEqual(tp.info_hash, lt.sha1_hash('abababababababababab')) self.assertEqual(tp.file_priorities, [0, 1, 1]) self.assertEqual(tp.peers, [('1.1.1.1', 1), ('2.2.2.2', 2)]) ses = lt.session({'alert_mask': lt.alert.category_t.all_categories}) h = ses.add_torrent(tp) h.connect_peer(('3.3.3.3', 3)) for i in range(0, 10): alerts = ses.pop_alerts() for a in alerts: print(a.message()) time.sleep(0.1)
def add_torrent(self, torrentdl, atp): # If we are collecting the torrent for this infohash, abort this first. with self.metainfo_lock: ltsession = self.get_session(atp.pop('hops', 0)) if 'ti' in atp: infohash = str(atp['ti'].info_hash()) elif 'url' in atp: infohash = binascii.hexlify(parse_magnetlink(atp['url'])[1]) else: raise ValueError('No ti or url key in add_torrent_params') if infohash in self.metainfo_requests: self._logger.info("killing get_metainfo request for %s", infohash) request_handle = self.metainfo_requests.pop(infohash)['handle'] if request_handle: ltsession.remove_torrent(request_handle, 0) # Check if we added this torrent before known = [str(h.info_hash()) for h in ltsession.get_torrents()] if infohash in known: self.torrents[infohash] = (torrentdl, ltsession) return ltsession.find_torrent(lt.sha1_hash(infohash)) # Otherwise, add it anew torrent_handle = ltsession.add_torrent(encode_atp(atp)) infohash = str(torrent_handle.info_hash()) if infohash in self.torrents: raise DuplicateDownloadException("This download already exists.") self.torrents[infohash] = (torrentdl, ltsession) self._logger.debug("added torrent %s", infohash) return torrent_handle
def test_read_resume_data(self): resume_data = lt.bencode({ 'file-format': 'libtorrent resume file', 'info-hash': 'abababababababababab', 'name': 'test', 'save_path': '.', 'peers': '\x01\x01\x01\x01\x00\x01\x02\x02\x02\x02\x00\x02', 'file_priority': [0, 1, 1]}) tp = lt.read_resume_data(resume_data) self.assertEqual(tp.name, 'test') self.assertEqual(tp.info_hash, lt.sha1_hash('abababababababababab')) self.assertEqual(tp.file_priorities, [0, 1, 1]) self.assertEqual(tp.peers, [('1.1.1.1', 1), ('2.2.2.2', 2)]) ses = lt.session({'alert_mask': lt.alert.category_t.all_categories}) h = ses.add_torrent(tp) h.connect_peer(('3.3.3.3', 3)) for i in range(0, 10): alerts = ses.pop_alerts() for a in alerts: print(a.message()) time.sleep(0.1)
def test_read_resume_data(self): resume_data = lt.bencode({ 'file-format': 'libtorrent resume file', 'info-hash': 'abababababababababab', 'name': 'test', 'save_path': '.', 'peers': '\x01\x01\x01\x01\x00\x01\x02\x02\x02\x02\x00\x02', 'file_priority': [0, 1, 1] }) tp = lt.read_resume_data(resume_data) self.assertEqual(tp.name, 'test') self.assertEqual(tp.info_hashes.v1, lt.sha1_hash('abababababababababab')) self.assertEqual(tp.file_priorities, [0, 1, 1]) self.assertEqual(tp.peers, [('1.1.1.1', 1), ('2.2.2.2', 2)]) ses = lt.session(settings) h = ses.add_torrent(tp) for attr in dir(tp): print('%s: %s' % (attr, getattr(tp, attr))) h.connect_peer(('3.3.3.3', 3)) for i in range(0, 10): alerts = ses.pop_alerts() for a in alerts: print(a.message()) time.sleep(0.1)
def actionFile(self, file_path, *args, **kwargs): if "info_hash" in self.get: print("info hash :", self.get["info_hash"]) print("File index :", self.get["file_index"]) info_hash = libtorrent.sha1_hash( bytes.fromhex(self.get["info_hash"])) h = session.find_torrent(info_hash) if h.is_valid(): ti = h.torrent_file() files = ti.files() for file_index in range(0, files.num_files()): file = ti.file_at(file_index) if file.path in file_path: # priotarize file (7 max priority) h.file_priority(file_index, 7) kwargs["block_size"] = ti.piece_length() print("piece_size : {}".format(kwargs["block_size"])) self.file = file kwargs["file_size"] = file.size kwargs["file_obj"] = TorrentFile(h, file, self) return super(UiRequestPlugin, self).actionFile(file_path, *args, **kwargs)
def test_normal_fixed_fields(self) -> None: piece = lib.get_random_bytes(20) similar = lib.get_random_bytes(20) info = { b"name": b"test.txt", b"pieces": piece, b"piece length": 16384, b"length": 1024, b"similar": [similar], } ti = lt.torrent_info({b"creation date": 12345, b"info": info}) self.assertEqual(ti.total_size(), 1024) self.assertEqual(ti.piece_length(), 16384) self.assertEqual(ti.num_pieces(), 1) self.assertEqual(ti.piece_size(-1), 16384) self.assertEqual(ti.piece_size(0), 1024) self.assertEqual(ti.piece_size(1), 16384) self.assertEqual(ti.hash_for_piece(0), piece) self.assertEqual(ti.num_files(), 1) self.assertTrue(ti.is_valid()) self.assertFalse(ti.priv()) self.assertEqual(ti.info_section(), lt.bencode(info)) self.assertEqual(ti.creation_date(), 12345) self.assertEqual(ti.similar_torrents(), [lt.sha1_hash(similar)])
def test_read_resume_data(self): resume_data = lt.bencode( { "file-format": "libtorrent resume file", "info-hash": "abababababababababab", "name": "test", "save_path": ".", "peers": "\x01\x01\x01\x01\x00\x01\x02\x02\x02\x02\x00\x02", "file_priority": [0, 1, 1], } ) tp = lt.read_resume_data(resume_data) self.assertEqual(tp.name, "test") self.assertEqual(tp.info_hash, lt.sha1_hash("abababababababababab")) self.assertEqual(tp.file_priorities, [0, 1, 1]) self.assertEqual(tp.peers, [("1.1.1.1", 1), ("2.2.2.2", 2)]) ses = lt.session({"alert_mask": lt.alert.category_t.all_categories}) h = ses.add_torrent(tp) h.connect_peer(("3.3.3.3", 3)) for i in range(0, 10): alerts = ses.pop_alerts() for a in alerts: print(a.message()) time.sleep(0.1)
def get_metainfo(self, infohash_or_magnet, callback, timeout=30, timeout_callback=None, notify=True): magnet = infohash_or_magnet if infohash_or_magnet.startswith('magnet') else None infohash_bin = infohash_or_magnet if not magnet else parse_magnetlink(magnet)[1] infohash = hexlify(infohash_bin) if infohash in self.torrents: return with self.metainfo_lock: self._logger.debug('get_metainfo %s %s %s', infohash_or_magnet, callback, timeout) cache_result = self._get_cached_metainfo(infohash) if cache_result: callback(deepcopy(cache_result)) elif infohash not in self.metainfo_requests: # Flags = 4 (upload mode), should prevent libtorrent from creating files atp = {'save_path': self.metadata_tmpdir, 'flags': (lt.add_torrent_params_flags_t.flag_upload_mode)} if magnet: atp['url'] = magnet else: atp['info_hash'] = lt.sha1_hash(infohash_bin) try: handle = self.ltsession_metainfo.add_torrent(encode_atp(atp)) except TypeError as e: self._logger.warning("Failed to add torrent with infohash %s, " "attempting to use it as it is and hoping for the best", hexlify(infohash_bin)) self._logger.warning("Error was: %s", e) atp['info_hash'] = infohash_bin handle = self.ltsession_metainfo.add_torrent(encode_atp(atp)) if notify: self.notifier.notify(NTFY_TORRENTS, NTFY_MAGNET_STARTED, infohash_bin) self.metainfo_requests[infohash] = {'handle': handle, 'callbacks': [callback], 'timeout_callbacks': [timeout_callback] if timeout_callback else [], 'notify': notify} # if the handle is valid and already has metadata which is the case when torrent already exists in # session then metadata_received_alert is not fired so we call self.got_metainfo() directly here if handle.is_valid() and handle.has_metadata(): self.got_metainfo(infohash, timeout=False) return def schedule_call(): self.register_anonymous_task("schedule_got_metainfo_lookup", reactor.callLater(timeout, lambda: self.got_metainfo(infohash, timeout=True))) reactor.callFromThread(schedule_call) else: self.metainfo_requests[infohash]['notify'] = self.metainfo_requests[infohash]['notify'] and notify callbacks = self.metainfo_requests[infohash]['callbacks'] if callback not in callbacks: callbacks.append(callback) else: self._logger.debug('get_metainfo duplicate detected, ignoring')
def test_sha1(self) -> None: sha1 = lt.sha1_hash(lib.get_random_bytes(20)) ih = lt.info_hash_t(sha1) self.assertEqual(ih.get(lt.protocol_version.V1), sha1) self.assertEqual(ih.get(lt.protocol_version.V2), lt.sha1_hash()) self.assertEqual(ih.get_best(), sha1) self.assertTrue(ih.has(lt.protocol_version.V1)) self.assertFalse(ih.has(lt.protocol_version.V2)) self.assertTrue(ih.has_v1()) self.assertFalse(ih.has_v2()) self.assertEqual(ih.v1, sha1) self.assertEqual(ih.v2, lt.sha256_hash()) self.assertEqual(ih, lt.info_hash_t(sha1)) self.assertEqual(hash(ih), hash(lt.info_hash_t(sha1))) self.assertNotEqual(ih, lt.info_hash_t())
def get_valid_handle(core, tid): """ Either returns a valid torrent_handle or aborts with a client-error (400) """ handle = core.session.find_torrent(lt.sha1_hash(binascii.unhexlify(tid))) if not handle.is_valid(): raise web.HTTPNotFound(reason='Torrent not found: ' + tid) return handle
def test_sha256(self) -> None: sha256 = lt.sha256_hash(lib.get_random_bytes(32)) sha256_trunc = lt.sha1_hash(sha256.to_bytes()[:20]) ih = lt.info_hash_t(sha256) self.assertEqual(ih.get(lt.protocol_version.V1), lt.sha1_hash()) self.assertEqual(ih.get(lt.protocol_version.V2), sha256_trunc) self.assertEqual(ih.get_best(), sha256_trunc) self.assertFalse(ih.has(lt.protocol_version.V1)) self.assertTrue(ih.has(lt.protocol_version.V2)) self.assertFalse(ih.has_v1()) self.assertTrue(ih.has_v2()) self.assertEqual(ih.v1, lt.sha1_hash()) self.assertEqual(ih.v2, sha256) self.assertEqual(ih, lt.info_hash_t(sha256)) self.assertEqual(hash(ih), hash(lt.info_hash_t(sha256))) self.assertNotEqual(ih, lt.info_hash_t())
def test_similar(self) -> None: fs = lt.file_storage() fs.add_file("test.txt", 1024) ct = lt.create_torrent(fs) ct.set_hash(0, lib.get_random_bytes(20)) sha1 = lt.sha1_hash(lib.get_random_bytes(20)) ct.add_similar_torrent(sha1) entry = ct.generate() self.assertEqual(entry[b"info"][b"similar"], [sha1.to_bytes()])
def load_torrent(self, info_hash): """load torrent""" try: handle = self.session.find_torrent(libtorrent.sha1_hash(binascii.unhexlify(info_hash))) if handle.is_valid(): handle.prioritize_pieces(handle.get_torrent_info().num_pieces() * [TorrentStream.LOW]) return {'status': '{} loading'.format(info_hash)} except TypeError: return {'error': '{} incorrect hash'.format(info_hash)} return {'error': '{} not found'.format(info_hash)}
def get_valid_handle(tid): """ Either returns a valid torrent_handle or aborts with a client-error (400) """ handle = core.session.find_torrent(lt.sha1_hash(binascii.unhexlify(tid))) if not handle.is_valid(): raise HttpProcessingError( code=404, message='Torrent not found: ' + tid) return handle
def test_torrent_handle(self) -> None: atp = lt.add_torrent_params() atp.info_hashes = lt.info_hash_t( lt.sha1_hash(bytes.fromhex(self.info_hash_sha1))) session = lt.session(lib.get_isolated_settings()) with tempfile.TemporaryDirectory() as path: atp.save_path = path handle = session.add_torrent(atp) uri = lt.make_magnet_uri(handle) self.assertEqual(uri, f"magnet:?xt=urn:btih:{self.info_hash_sha1}")
def actionHavePiece(self, to, info_hash, piece_index): if not self.hasSitePermission(self.site.address): return self.response(to, {"error": "Forbidden"}) info_hash = libtorrent.sha1_hash(bytes.fromhex(info_hash)) h = session.find_torrent(info_hash) if h.is_valid(): response = h.have_piece(piece_index) self.response(to, response) else: self.response(to, {'error': 'Torrent not found'})
def actionTorrentStatus(self, to, info_hash): info_hash = libtorrent.sha1_hash(info_hash.decode('hex')) h = self.session.find_torrent(info_hash) if h.is_valid(): s = h.status() self.response(to, {'progress': s.progress, \ 'download_rate': s.download_rate, \ 'upload_rate': s.upload_rate, \ 'num_peers': s.num_peers, \ 'state': str(s.state) }) else: self.response(to, {'error': 'Torrent not found'})
def pause_torrent(self, info_hash): """pause/resume torrent""" try: handle = self.session.find_torrent(libtorrent.sha1_hash(binascii.unhexlify(info_hash))) if handle.is_valid(): if handle.status().paused: handle.resume() return {'status': '{} resumed'.format(info_hash)} else: handle.pause() return {'status': '{} paused'.format(info_hash)} except TypeError: return {'error': '{} incorrect hash'.format(info_hash)} return {'error': '{} not found'.format(info_hash)}
def actionTorrentStatus(self, to, info_hash): if not self.hasSitePermission(self.site.address): return self.response(to, {"error": "Forbidden"}) info_hash = libtorrent.sha1_hash(bytes.fromhex(info_hash)) h = session.find_torrent(info_hash) if h.is_valid(): s = h.status() self.response(to, {'progress': s.progress, \ 'download_rate': s.download_rate, \ 'upload_rate': s.upload_rate, \ 'num_peers': s.num_peers, \ 'state': str(s.state) }) else: self.response(to, {'error': 'Torrent not found'})
def remove_torrent(self, info_hash): """remove torrent from list by info_hash""" try: handle = self.session.find_torrent(libtorrent.sha1_hash(binascii.unhexlify(info_hash))) if handle.is_valid(): ti = handle.get_torrent_info() if ti: fastresume = handle.save_path() + "/" + ti.name() + '.fastresume' if os.path.exists(fastresume): os.remove(fastresume) self.session.remove_torrent(handle, libtorrent.options_t.delete_files) return {'status': '{} removed'.format(info_hash)} except TypeError: return {'error': '{} incorrect hash'.format(info_hash)} return {'error': '{} not found'.format(info_hash)}
def actionPrioritizePiece(self, to, info_hash, piece_index, new_priority): if not self.hasSitePermission(self.site.address): return self.response(to, {"error": "Forbidden"}) info_hash = libtorrent.sha1_hash(bytes.fromhex(info_hash)) h = session.find_torrent(info_hash) if h.is_valid(): if 0 <= new_priority <= 7: h.piece_priority(piece_index, new_priority) self.response(to, 'ok') else: self.response(to, { 'error': 'new_priority should be an integer bewteen 0 and 7' }) else: self.response(to, {'error': 'Torrent not found'})
def actionGetTorrentInfo(self, to, info_hash): info_hash = libtorrent.sha1_hash(info_hash.decode('hex')) h = self.session.find_torrent(info_hash) if h.is_valid(): ti = h.get_torrent_info() files = ti.files() arrayFiles = [] for file in files: arrayFiles.append(file.path) self.response(to, {'name': ti.name(), \ 'num_files' : ti.num_files(), \ 'files': arrayFiles \ }) else: self.response(to, {'error': 'Torrent not found'})
def test_add_torrent_info_hash(self): s = lt.session(settings) h = s.add_torrent({ 'info_hash': b'a' * 20, 'info_hashes': b'a' * 32, 'save_path': '.' }) time.sleep(1) alerts = s.pop_alerts() while len(alerts) > 0: a = alerts.pop(0) print(a) self.assertTrue(h.is_valid()) self.assertEqual( h.status().info_hashes, lt.info_hash_t(lt.sha1_hash(b'a' * 20), lt.sha256_hash(b'a' * 32)))
def test_dual(self) -> None: sha1 = lt.sha1_hash(lib.get_random_bytes(20)) sha256 = lt.sha256_hash(lib.get_random_bytes(32)) # type: ignore ih = lt.info_hash_t(sha1, sha256) # type: ignore self.assertEqual(ih.get(lt.protocol_version.V1), sha1) self.assertEqual(ih.get(lt.protocol_version.V2), sha256) self.assertEqual(ih.get_best(), sha256) self.assertTrue(ih.has(lt.protocol_version.V1)) self.assertTrue(ih.has(lt.protocol_version.V2)) self.assertTrue(ih.has_v1()) self.assertTrue(ih.has_v2()) self.assertEqual(ih.v1, sha1) self.assertEqual(ih.v2, sha256) self.assertEqual(ih, lt.info_hash_t(sha1, sha256)) # type: ignore self.assertEqual(hash(ih), hash(lt.info_hash_t(sha1, sha256))) # type: ignore self.assertNotEqual(ih, lt.info_hash_t())
def get_health(self, infohash, timeout=15): """ Lookup the health of a given infohash. :param infohash: The 20-byte infohash to lookup. :param timeout: The timeout of the lookup. :return: A Deferred that fires with a tuple, indicating the number of seeders and peers respectively. """ if infohash in self.lookup_deferreds: return self.lookup_deferreds[infohash] lookup_deferred = Deferred() self.lookup_deferreds[infohash] = lookup_deferred self.bf_seeders[infohash] = bytearray(256) self.bf_peers[infohash] = bytearray(256) # Perform a get_peers request. This should result in get_peers responses with the BEP33 bloom filters. self.lt_session.dht_get_peers(lt.sha1_hash(infohash)) self.register_task("lookup_%s" % hexlify(infohash), reactor.callLater(timeout, self.finalize_lookup, infohash)) return lookup_deferred
def get_health(self, infohash, timeout=15): """ Lookup the health of a given infohash. :param infohash: The 20-byte infohash to lookup. :param timeout: The timeout of the lookup. :return: A Deferred that fires with a tuple, indicating the number of seeders and peers respectively. """ if infohash in self.lookup_deferreds: return self.lookup_deferreds[infohash] lookup_deferred = Deferred() self.lookup_deferreds[infohash] = lookup_deferred self.bf_seeders[infohash] = bytearray(256) self.bf_peers[infohash] = bytearray(256) # Perform a get_peers request. This should result in get_peers responses with the BEP33 bloom filters. self.lt_session.dht_get_peers(lt.sha1_hash(infohash)) self.register_task( "lookup_%s" % hexlify(infohash), reactor.callLater(timeout, self.finalize_lookup, infohash)) return lookup_deferred
def actionGetTorrentInfo(self, to, info_hash): if not self.hasSitePermission(self.site.address): return self.response(to, {"error": "Forbidden"}) info_hash = libtorrent.sha1_hash(bytes.fromhex(info_hash)) h = session.find_torrent(info_hash) if h.is_valid(): ti = h.get_torrent_info() files = ti.files() arrayFiles = [] for file in files: arrayFiles.append({'path': file.path, \ 'offset': file.offset, \ 'size': file.size \ }) self.response(to, {'name': ti.name(), \ 'num_files' : ti.num_files(), \ 'files': arrayFiles, \ 'piece_length': ti.piece_length() \ }) else: self.response(to, {'error': 'Torrent not found'})
def test_sha1hash(self): h = 'a0'*20 s = lt.sha1_hash(binascii.unhexlify(h)) self.assertEqual(h, str(s))
def do_dht_lookup(self): session.dht_get_peers(self.infohash) def start(self, infohash): self.infohash = infohash lc = LoopingCall(self.process_alerts) lc.start(2) session.add_dht_router('router.bittorrent.com', 6881) session.add_dht_router('router.utorrent.com', 6881) session.add_dht_router('router.bitcomet.com', 6881) session.start_dht() reactor.callLater(10, self.do_dht_lookup) reactor.callLater(20, self.determine_health) if __name__ == "__main__": if len(sys.argv) == 1: print "You should provide an infohash!" exit(1) print "Will check DHT health of infohash %s" % sys.argv[1] infohash = sys.argv[1].decode('hex') sha1_infohash = lt.sha1_hash(infohash) swarm_health_manager = SwarmHealthManager() reactor.callWhenRunning(swarm_health_manager.start, sha1_infohash) reactor.run()
def test_sha1_hash(self) -> None: sha1 = lt.sha1_hash(lib.get_random_bytes(20)) ti = lt.torrent_info(sha1) self.assertEqual(ti.info_hash(), sha1)
def test_sha1_hash_deprecated(self) -> None: sha1 = lt.sha1_hash(lib.get_random_bytes(20)) with self.assertWarns(DeprecationWarning): lt.torrent_info(sha1)