Search code examples
pythonlibtorrent

How to seed with python libtorrent DHT?


I have Ubuntu server with libtorrent, python-libtorrent and local pc Win10 with uTorrent.

On Server I want create 1.torrent and start seeding it on server

On PC I want load 1.torrent to uTorrent and download it using DHT

I made script: creation 1.torrent file, add it to DHT.

import libtorrent as lt
import sys, time


videoFile = "/tmp/1/1.mp4.gpg"
workingPath = "/tmp/1"

#Create torrent

f = lt.file_storage()
lt.add_files(f, videoFile)
t = lt.create_torrent(f)
t.add_node("router.utorrent.com", 6881)
t.add_node("dht.transmissionbt.com", 6881)
lt.set_piece_hashes(t, workingPath)
torrent = t.generate()
f = open("/tmp/1/1.torrent", "wb")
f.write(lt.bencode(torrent))
f.close()

#Seeding

PORT_RANGE = (6881,6891)
s = lt.session()
s.listen_on(PORT_RANGE[0],PORT_RANGE[1])
s.add_dht_router('router.utorrent.com',6881)
s.start_dht()
print "DHT start: ", s.is_dht_running()
print "DHT state: ", s.dht_state()


params = {
            'save_path': workingPath,
            'storage_mode': lt.storage_mode_t.storage_mode_sparse,
            'ti': lt.torrent_info(torrent),
            'seed_mode': True,
            'paused': False,
            'upload_mode':True,
            'super_seeding':True
        }
h = s.add_torrent(params)
print("Total size: " + str(h.status().total_wanted))
print("Name: " + h.name())
while True:
    s = h.status()
    msg = '\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s'
    print(msg % (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, s.num_peers, s.state))
    sys.stdout.flush()
    time.sleep(1)

output was

# python 1.py
DHT start:  True
DHT state:  {'node-id': 'HgH\xcd\xe3\xbc\x9b\xa9\x9b\xf6\xbf\xe7E\xd9\x1d.\xd9<\x838'}
Total size: 82500767
Name: 1.mp4.gpg
100.00% complete (down: 0.0 kb/s up: 0.0 kB/s peers: 0) seeding
0.00% complete (down: 0.0 kb/s up: 0.0 kB/s peers: 0) checking_resume_data
0.00% complete (down: 0.0 kb/s up: 0.0 kB/s peers: 0) checking_resume_data
0.00% complete (down: 0.0 kb/s up: 0.0 kB/s peers: 0) checking_resume_data
....
....
....
0.00% complete (down: 0.0 kb/s up: 0.0 kB/s peers: 0) checking_resume_data

Utorrent status on windows pc was 'Connecting to peers'

Why I can't seed torrent through libtorrent dht?


Solution

  • you're setting seed_mode to True. Which will make libtorrent assume you have all the files for the torrent, hence the state being "seeding".

    Once you get a peer, and it requests a block, libtorrent will actually go and open the file, and it will actually verify the piece hash as well (to avoid uploading corrupt data). However, if the file isn't there, the torrent will transition into checking mode, where it can no longer trust that it is seeding. It will check all the files.

    The first step in checking the files is checking resume data (which you transition into). However, this is typically very quick and is followed by transitioning into checking-files state.

    This doesn't seem to happen for some reason. To trouble shoot that you should pop_alerts() and print them to a log, and possibly enable more alerts, like torrent_log alerts.

    However, it appears the first problem you have is that you don't have the files you're trying to seed. It's very hard to diagnose this without the alert log though.