Skip to content

Instantly share code, notes, and snippets.

@harmonbc
Created December 7, 2012 13:32
Show Gist options
  • Save harmonbc/4233304 to your computer and use it in GitHub Desktop.
Save harmonbc/4233304 to your computer and use it in GitHub Desktop.
Print out all Torrent IP Addresses
#!/usr/bin/python
#Heavily based on libtorrent example
#Did this to print examples of what information is being processed at different parts of the bittorrent protocol
import libtorrent as lt
import time
import sys
class SmallClient:
def __init__(self):
global ses
ses = lt.session()
ses.listen_on(6881, 6891)
def getPeers(self,arg):
#Use bencode to read the contents of the file
e = lt.bdecode(open(arg, 'rb').read())
#Build torrent info into libtorrent
info = lt.torrent_info(e)
#Add the torrent info to the current session
h = ses.add_torrent({'ti':info,'save_path':'./'})
#Print the torrent information
print "File: "+h.name()
print "Hash: "+str(h.info_hash())
print "Trackers: "
t = h.trackers()
for s in t:
print str(s['url'])
raw_input("Press Enter to Continue...")
print "-----PEERS-----"
while (not h.is_seed()):
count = 0
#Get peer information from the torrent
p = h.get_peer_info()
#for every item i in the peer list
for i in p:
count+=1
#print their IP address
u, v = i.ip
print str(count)+") "+str(u)
time.sleep(5)
#Build the client object
client = SmallClient()
#Run the client so we can see the IP addresses
client.getPeers(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment