Skip to content

Instantly share code, notes, and snippets.

@ardhipoetra
Last active April 18, 2016 09:50
Show Gist options
  • Save ardhipoetra/e9ab3a94cf576c0208fcdca97560619b to your computer and use it in GitHub Desktop.
Save ardhipoetra/e9ab3a94cf576c0208fcdca97560619b to your computer and use it in GitHub Desktop.
DHT test on libtorrent-python binding
import libtorrent as lt
import time
infohash_str = "5aec5ee9f2d044316fe1de29d221452103ceb958"
print "version : %s" %lt.version
print "start session"
ses = lt.session()
ses.listen_on(6881, 6891)
ses.set_alert_mask(lt.alert.category_t.stats_notification |
lt.alert.category_t.dht_operation_notification |
lt.alert.category_t.dht_notification)
print "start DHT"
ses.start_dht()
print "is DHT running? %s" %(ses.is_dht_running())
print "adding router"
ses.add_dht_router('router.bittorrent.com', 6881)
ses.add_dht_router('router.utorrent.com', 6881)
ses.add_dht_router('router.bitcomet.com', 6881)
print "infohash : %s" %(infohash_str)
ihash_sh1 = lt.sha1_hash(infohash_str)
print "get peers called"
ses.dht_get_peers(ihash_sh1)
print "waiting for alerts"
stopped = False
count = 0
while(not stopped):
al = ses.pop_alerts()
if not len(al):
print "no alerts, sleep for 5"
time.sleep(5)
# furiously ask for peers!
ses.dht_get_peers(ihash_sh1)
else:
print "there are %d alerts" %(len(al))
for a in al:
if a.category() & lt.alert.category_t.dht_operation_notification:
print "DHTOP : %s" %(a)
# this category only used in "dht_get_peers_reply_alert"
print a.info_hash
print a.num_peers()
for p in a.peers():
print p
count += 1
# elif a.category() & lt.alert.category_t.dht_notification:
# print "DHTNFY : %s" %(a)
else :
ses.dht_get_peers(ihash_sh1)
time.sleep(1)
# we've seen enough
if count >= 30:
stopped = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment