Skip to content

Instantly share code, notes, and snippets.

@ardhipoetra
Last active March 29, 2016 10:03
Show Gist options
  • Save ardhipoetra/457b35882d16289b4c50 to your computer and use it in GitHub Desktop.
Save ardhipoetra/457b35882d16289b4c50 to your computer and use it in GitHub Desktop.
Libtorrent python binding - get peer demo
import libtorrent as lt
import time
import pprint
import sys
def create_peerlist_data(peer_info):
peer_dict = {}
peer_dict['id'] = peer_info.pid
peer_dict['extended_version'] = peer_info.client
peer_dict['ip'] = peer_info.ip[0]
peer_dict['port'] = peer_info.ip[1]
# optimistic_unchoke = 0x800 seems unavailable in python bindings
peer_dict['optimistic'] = bool(peer_info.flags & 2048)
peer_dict['direction'] = 'L' if bool(peer_info.flags & peer_info.local_connection) else 'R'
peer_dict['uprate'] = peer_info.payload_up_speed
peer_dict['uinterested'] = bool(peer_info.flags & peer_info.remote_interested)
peer_dict['uchoked'] = bool(peer_info.flags & peer_info.remote_choked)
peer_dict['uhasqueries'] = peer_info.upload_queue_length > 0
peer_dict['uflushed'] = peer_info.used_send_buffer > 0
peer_dict['downrate'] = peer_info.payload_down_speed
peer_dict['dinterested'] = bool(peer_info.flags & peer_info.interesting)
peer_dict['dchoked'] = bool(peer_info.flags & peer_info.choked)
# snubbed = 0x1000 seems unavailable in python bindings
peer_dict['snubbed'] = bool(peer_info.flags & 4096)
peer_dict['utotal'] = peer_info.total_upload
peer_dict['dtotal'] = peer_info.total_download
peer_dict['completed'] = peer_info.progress
peer_dict['have'] = peer_info.pieces
peer_dict['speed'] = peer_info.remote_dl_rate
peer_dict['country'] = peer_info.country
peer_dict['connection_type'] = peer_info.connection_type
# add upload_only and/or seed
peer_dict['seed'] = bool(peer_info.flags & peer_info.seed)
peer_dict['upload_only'] = bool(peer_info.flags & peer_info.upload_only)
return peer_dict
settings = {}
settings['enable_outgoing_utp'] = True
settings['enable_incoming_utp'] = True
settings['enable_outgoing_tcp'] = False
settings['enable_incoming_tcp'] = False
ses = lt.session()
ses.set_settings(settings)
ses.listen_on(6881, 6891)
ses.start_dht()
info = lt.torrent_info(sys.argv[1])
h = ses.add_torrent({'ti': info, 'save_path': '.'})
while (not h.is_seed()):
s = h.status()
peerlist = h.get_peer_info()
# use len of peerlist instead of num_peers in status
print "got %d peers" %len(peerlist) if peerlist else 0
for p in peerlist:
peer = create_peerlist_data(p)
out = "%s ip:%s uprate:%s dwnrate:%s progress:%s seed/ul:%s/%s>> " \
%(str(s.info_hash), peer['ip'], peer['uprate'], peer['downrate'],
peer['completed'],peer['seed'], peer['upload_only'])
if peer['uinterested']:
out += "INTERESTED in us |"
if peer['uchoked']:
out += "CHOKED on US |"
if peer['dinterested']:
out += "we are INTERESTED |"
if peer['dchoked']:
out += "we CHOKED him |"
print out
state_str = ['queued', 'checking', 'downloading metadata', \
'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
print('\r%.2f%% complete (down: %.1f kB/s up: %.1f kB/s peers: %d) %s' % \
(s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
s.num_peers, state_str[s.state]))
sys.stdout.flush()
time.sleep(1)
print(h.name(), 'complete')
# nah, it can't work for now
# print ses.testfun()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment