[libtorrent] How to make a merkle torrent with Python binding?
Brought to you by:
arvidn
|
From: Sam K. <sa...@ea...> - 2015-11-30 03:51:44
|
Hi folks,
I've tried to share a file with libtorrent by making a merkle torrent.
Although made some progress, now I'm stuck.
The following snippet shows how I generate the torrent.
----
def make_torrent_info():
fs = lt.file_storage()
fname = 'out000.ts'
full_fname = os.path.join(tests_dir, fname)
size = os.path.getsize(full_fname)
fs.add_file(fname, size)
fs.set_name(fname)
flags = (lt.create_torrent_flags_t.merkle)
t = lt.create_torrent(fs, 16 * 1024, -1, flags=0)
t.set_creator('libtorrent %s' % lt.version)
lt.set_piece_hashes(t, tests_dir)
entry = t.generate()
print(entry)
torrent_filename = os.path.join(tests_dir, 'out.torrent')
with open(torrent_filename, 'wb+') as f:
print(lt.bencode(entry), file=f)
ti = lt.torrent_info(entry)
return ti
def share_file(session):
atp = {}
atp["save_path"] = tests_dir
atp["seed_mode"] = True
atp["storage_mode"] = lt.storage_mode_t.storage_mode_sparse
atp["paused"] = False
atp["auto_managed"] = True
ti = make_torrent_info()
atp['ti'] = ti
mt = ti.merkle_tree()
assert mt is not None
assert len(mt) > ti.num_pieces()
handle = session.add_torrent(atp)
assert handle.has_metadata()
handle.force_recheck() # this is to make the checking happen
immediately.
-----
Testing environment:
OS: Mac OSX 10.10 x86-64
Libtorrent: 1.0.6 installed with Homebrew.
The question is:
If I set flags = lt.create_torrent_flags_t.merkle instead of 0, the file
cannot be downloaded by other clients.
>From the log messages, the torrent's state changed from 'seeding' to
'downloading' if it's a merkle torrent.
And, after changing state to 'downloading', the progress became 1.29%
instead of 100%.
I guess this's caused by merkle hash tree not complete in the torrent, but
totally have no idea how to fix it.
Can anyone please tell me what's wrong with the code or share a working
example?
Thanks in advance,
Sam
|