4
votes

I search to retrieve the peer's IP of a torrent using libtorrent with Python. I try with the code :

import libtorrent as lt
import time

ses = lt.session()
ses.listen_on(6881, 6891)
info = lt.torrent_info('test.torrent')
h = ses.add_torrent({'ti': info, 'save_path': './'})
print 'starting', h.name()
while (not h.is_seed()):
    s = h.status()
    p = h.get_peer_info()
    for i in p:
        print i.ip

    print "\n\n"

    sys.stdout.flush()

    time.sleep(1)

print h.name(), 'complete'

It works more or less but I have two problems:

  1. The torrent are downloaded.
  2. The loop are executed dozens times before I receive one peer list.

Can you help me?

Thank you so much.

1

1 Answers

7
votes

There is no one canonical "peer list". There are the peers you're currently connected to. There is however "every peer in the swarm".

libtorrent can tell you the peers you're connected to, which means they completed a uTP or TCP 3-way handshake.

it typically takes some time to ramp up peer connections. You need to find out about peers, you need to try to connect to them and they need to be up and not have a full peer list. This is why you're not connected to a bunch of peers instantly.

It sounds like you're interested in all peers in the swarm. You are not very likely to find every peer. Peers may not announce to the same trackers, and find each other via PEX or DHT. Many peers are not connectable, and the only way to find them is to have them find you, which they may not be interested in.

Now, it's not clear why you would want peer IPs if you're not interested in downloading the torrent. Given that you aren't, why connect to them at all?

You could simply call get_full_peer_list(), however, that's not available in the python bindings. You could also just announce to the tracker over and over again and collect the resulting IPs.