本文整理汇总了Python中libtorrent.bencode方法的典型用法代码示例。如果您正苦于以下问题:Python libtorrent.bencode方法的具体用法?Python libtorrent.bencode怎么用?Python libtorrent.bencode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类libtorrent
的用法示例。
在下文中一共展示了libtorrent.bencode方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_torrent
# 需要导入模块: import libtorrent [as 别名]
# 或者: from libtorrent import bencode [as 别名]
def generate_torrent(incl_file: pathlib.Path, resource_hash: str) -> dict:
"""Generate torrent file for a given file and write it to disk
:param pathlib.Path incl_file: file to include in torrent
:param str resource_hash: resource hash
:rtype: tuple
:return: (torrent file as pathlib, torrent file data sha1 hash)
"""
fs = libtorrent.file_storage()
libtorrent.add_files(fs, str(incl_file))
tor = libtorrent.create_torrent(fs)
tor.set_creator('libtorrent {}'.format(libtorrent.version))
libtorrent.set_piece_hashes(tor, str(incl_file.parent))
torrent = tor.generate()
torrent_data = libtorrent.bencode(torrent)
fp = _TORRENT_DIR / '{}.torrent'.format(resource_hash)
with fp.open('wb') as f:
f.write(torrent_data)
return fp, hashlib.sha1(torrent_data).hexdigest()
示例2: get
# 需要导入模块: import libtorrent [as 别名]
# 或者: from libtorrent import bencode [as 别名]
def get(self):
response = {}
info_hashes = self.get_arguments('info_hash')
for info_hash in info_hashes:
info_hash = str(info_hash)
response[info_hash] = {}
numwant = 100
compact = True
no_peer_id = 1
complete, incomplete, _ = self.get_peers(info_hash, numwant,
compact, no_peer_id,
self.tracker_interval * 2)
response[info_hash]['complete'] = complete
response[info_hash]['downloaded'] = complete
response[info_hash]['incomplete'] = incomplete
self.set_header('content-type', 'text/plain')
self.write(bencode(response))
self.finish()
示例3: make_torrent
# 需要导入模块: import libtorrent [as 别名]
# 或者: from libtorrent import bencode [as 别名]
def make_torrent(path, save):
fs = lt.file_storage()
lt.add_files(fs, path)
if fs.num_files() == 0:
print 'no files added'
sys.exit(1)
input = os.path.abspath(path)
basename = os.path.basename(path)
t = lt.create_torrent(fs, 0, 4 * 1024 * 1024)
t.add_tracker("http://10.0.1.5:8760/announce")
t.set_creator('libtorrent %s' % lt.version)
lt.set_piece_hashes(t, os.path.split(input)[0], lambda x: sys.stderr.write('.'))
sys.stderr.write('\n')
save = os.path.dirname(input)
save = "%s/%s.torrent" % (save, basename)
f=open(save, "wb")
f.write(lt.bencode(t.generate()))
f.close()
print "the bt torrent file is store at %s" % save
示例4: getCompleteList
# 需要导入模块: import libtorrent [as 别名]
# 或者: from libtorrent import bencode [as 别名]
def getCompleteList(self, opt, ui, progress, tmp_dir, hist_folder):
m = ['Not Able To Open']
if opt == 'Open':
MainWindow = QtWidgets.QWidget()
item, ok = QtWidgets.QInputDialog.getText(ui, 'Input Dialog', 'Enter Torrent Url or Magnet Link or local torrent file path')
if ok and item:
if (item.startswith('http') or (os.path.isfile(item) and item.endswith('.torrent'))):
home = hist_folder
name1 = os.path.basename(item).replace('.torrent', '')
torrent_dest1 = os.path.join(tmp_dir, name1+'.torrent')
if not os.path.exists(torrent_dest1):
if item.startswith('http'):
ccurl(item+'#'+'-o'+'#'+torrent_dest1)
else:
shutil.copy(item, torrent_dest1)
if os.path.exists(torrent_dest1):
info = lt.torrent_info(torrent_dest1)
name = info.name()
torrent_dest = os.path.join(home, name+'.torrent')
shutil.copy(torrent_dest1, torrent_dest)
m = [name]
elif item.startswith('magnet:'):
torrent_handle, stream_session, info = get_torrent_info_magnet(item, tmp_dir, ui, progress, tmp_dir)
torrent_file = lt.create_torrent(info)
home = hist_folder
name = info.name()
torrent_dest = os.path.join(home, name+'.torrent')
with open(torrent_dest, "wb") as f:
f.write(lt.bencode(torrent_file.generate()))
torrent_handle.pause()
stream_session.pause()
m = [name]
m.append(1)
return m
示例5: save_resume
# 需要导入模块: import libtorrent [as 别名]
# 或者: from libtorrent import bencode [as 别名]
def save_resume(self):
"""
Función encargada guardar los metadatos para continuar una descarga mas rapidamente
"""
if self._th.need_save_resume_data() and self._th.is_valid() and self.meta:
r = ResumeData(self)
start=time.time()
while (time.time() - start) <= 5 :
if r.data or r.failed:
break
time.sleep(0.1)
if r.data:
self._cache.save_resume(self.unique_file_id,lt.bencode(r.data))
示例6: file_complete
# 需要导入模块: import libtorrent [as 别名]
# 或者: from libtorrent import bencode [as 别名]
def file_complete(self, torrent):
info_hash=str(torrent.info_hash())
nt=lt.create_torrent(torrent)
tname=self._tname(info_hash)
f = open(tname, 'wb')
f.write(lt.bencode(nt.generate()))
f.close()
示例7: create_torrent
# 需要导入模块: import libtorrent [as 别名]
# 或者: from libtorrent import bencode [as 别名]
def create_torrent(self):
# TODO check if root
tarball_uid = self.get('tarball')
if not tarball_uid:
self.log.error("No tarball in DB.")
return False
cluster = Cluster(mongo_db=self._mongo_db)
tarball = cluster.get('path') + "/torrents/" + tarball_uid + ".tgz"
if not os.path.exists(tarball):
self.log.error("Wrong path in DB.")
return False
tracker_address = cluster.get('frontend_address')
if tracker_address == '':
self.log.error("Tracker address needs to be configured.")
return False
tracker_port = cluster.get('frontend_port')
if tracker_port == 0:
self.log.error("Tracker port needs to be configured.")
return False
user = cluster.get('user')
user_id = pwd.getpwnam(user).pw_uid
grp_id = pwd.getpwnam(user).pw_gid
old_cwd = os.getcwd()
os.chdir(os.path.dirname(tarball))
uid = str(uuid.uuid4())
torrentfile = cluster.get('path') + "/torrents/" + uid + ".torrent"
fs = libtorrent.file_storage()
libtorrent.add_files(fs, os.path.basename(tarball))
t = libtorrent.create_torrent(fs)
if cluster.get('frontend_https'):
proto = 'https'
else:
proto = 'http'
t.add_tracker((proto + "://" + str(tracker_address) +
":" + str(tracker_port) + "/announce"))
t.set_creator(torrent_key)
t.set_comment(uid)
libtorrent.set_piece_hashes(t, ".")
f = open(torrentfile, 'w')
f.write(libtorrent.bencode(t.generate()))
f.close()
os.chown(torrentfile, user_id, grp_id)
self.set('torrent', str(uid))
os.chdir(old_cwd)
return True