Retrieve peers list without download the torrent using python-libtorrent
Asked Answered
M

1

5

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.

Mure answered 14/3, 2013 at 16:42 Comment(0)
A
7

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.

Activator answered 15/3, 2013 at 4:2 Comment(7)
I want to retrieve the peer list for measurements for my master thesis. What do you mean by "You could also just announce to the tracker over and over again and collect the resulting IPs." ? Have you some code or some function name to do that ? Thank you very much.Mure
there's no free standing tracker announce function in libtorrent, but you could potentially use urllib. then again, you wouldn't get DHT, or UDP tracker peers.Activator
Does get_full_peer_list() return those that are discovered via PEX or DHT?Loar
yes. It returns all peers that the client believes are part of the swarm, regardless of how it learned about them. There's also local peer discovery for instance.Activator
@Activator get_full_peer_list is deprecated. Is there an alternative to that?Bechuanaland
it's not a perfect substitute, but you could save resume data and look in the peers and banned_peers fields of the resulting add_torrent_paramsActivator
@Activator running torrent_handle.get_peer_info(&std::vector<libtorrent::peer_info>) while add resume alert is giving some peers where torrent_handle = alert__-> handle Is this reliable? any difference ?Bechuanaland

© 2022 - 2024 — McMap. All rights reserved.