def dhtcrawler(selfaddr, bootaddr, bootinfo): MAX_WORKING_TIME = 60*10 dht = DHT(host=selfaddr[0], port=selfaddr[1]) dht.start() dht.bootstrap(bootaddr[0], bootaddr[1]) dht.ht.add_hash(bootinfo) time.sleep(MAX_WORKING_TIME) dht.stop() return
def get_peers(self, magnet, iterations=5): dht = DHT(host='0.0.0.0', port=0) dht.start() dht.bootstrap('router.bittorrent.com', 6881) dht.ht.add_hash(magnet.decode("hex")) result = [] for count in xrange(iterations): peers = dht.ht.get_hash_peers(magnet.decode("hex")) for peer in peers: result.append((peer)) time.sleep(3) dht.stop() return [p[0] for p in list(set(result))]
def instance(cls, name="SingleThread"): """Argument `name` accepted `SingleThread` or `MultiThread`.""" # globals()[name] if not hasattr(cls, '_options'): cls._options = dict() host = cls._options.get('host', '0.0.0.0') port = cls._options.get('port', 6881) node_size = cls._options.get('node_size', 2**160) if name == "SingleThread": return DHT(host, port, node_size) if name == "MultiThread": thread_num = cls._options.get('thread_num', 8) q_size = cls._options.get('q_size', 200) return ThreadSupport(DHT, thread_num, q_size)(host, port, node_size) else: raise ValueError( 'Excepted "SingleThread" or "MultiThread", got "%s".' % name)
if __name__ == "__main__": # Enable logging loglevel = logging.DEBUG formatter = logging.Formatter("[%(levelname)s@%(created)s] %(message)s") stdout_handler = logging.StreamHandler() stdout_handler.setFormatter(formatter) logging.getLogger("btdht").setLevel(loglevel) logging.getLogger("btdht").addHandler(stdout_handler) logger = logging.getLogger(__name__) logger.setLevel(loglevel) logger.addHandler(stdout_handler) # Start DHT Node no port 60000 dht = DHT(host='0.0.0.0', port=60000) dht.start() # Boostrap it dht.bootstrap('router.bittorrent.com', 6881) # Find me peers for that torrent hashes dht.ht.add_hash("746385fe32b268d513d068f22c53c46d2eb34a5c".decode("hex")) #dht.ht.add_hash("c90db98c5aabe64dcd7f730d816e755242fffbd4".decode("hex")) #dht.ht.add_hash("a961bc2b93a2304880f919e304424a14400ba8a2".decode("hex")) for count in xrange(10): logger.info("DHT Nodes found: %d" % (dht.rt.count())) logger.info("Bad DHT nodes found: %d" % (dht.rt.bad_count())) logger.info("Total peers found: %d" % (dht.ht.count_all_peers()))
# Enable logging at DEBUG level loglevel = logging.DEBUG formatter = logging.Formatter("[%(levelname)s@%(created)s] %(message)s") stdout_handler = logging.StreamHandler() stdout_handler.setFormatter(formatter) logging.getLogger("btdht").setLevel(loglevel) logging.getLogger("btdht").addHandler(stdout_handler) logger = logging.getLogger(__name__) logger.setLevel(loglevel) logger.addHandler(stdout_handler) if __name__ == "__main__": # Start DHT Node on port 60000 dht = DHT(host='0.0.0.0', port=60000) dht.start() # Boostrap it dht.bootstrap('router.bittorrent.com', 6881) # Find me peers for that torrent hashes dht.ht.add_hash(current_magnet) res = [] for count in xrange(10): logger.info("DHT Nodes found: %d" % (dht.rt.count())) logger.info("Bad DHT nodes found: %d" % (dht.rt.bad_count())) logger.info("Total peers found: %d" % (dht.ht.count_all_peers())) # How many peers at this moment? peers = dht.ht.get_hash_peers(current_magnet)
file_handler = logging.FileHandler("HASH.log") file_handler.setFormatter(formatter) logging.getLogger("btdht").setLevel(fileLogLevel) logging.getLogger("btdht").addHandler(file_handler) logging.getLogger("std").setLevel(stdLogLevel) logging.getLogger("std").addHandler(stdout_handler) #thread number threadNumb=THREAD_NUMBER #working time for threads workingTime=WORKINGTIME #threads pool threads=[] for i in xrange(threadNumb): i=i+9500 thread = DHT(host='0.0.0.0', port=i) thread.start() thread.bootstrap('router.bittorrent.com',6881) CurrentMagnet = "4CDE5B50A8930315B479931F6872A3DB59575366" thread.ht.add_hash(CurrentMagnet.decode("hex")) threads.append(thread) print "start node %d"%i #time.sleep(2) while 1==1: time.sleep(3600) ''' dht.ht.hashes.clear() dht.rt.nodes.clear() '''
import logging import time import sys from btdht import DHT # Enable logging at DEBUG level loglevel = logging.DEBUG formatter = logging.Formatter("[%(levelname)s@%(created)s] %(message)s") stdout_handler = logging.StreamHandler() stdout_handler.setFormatter(formatter) logging.getLogger("btdht").setLevel(loglevel) logging.getLogger("btdht").addHandler(stdout_handler) logger = logging.getLogger(__name__) logger.setLevel(loglevel) logger.addHandler(stdout_handler) if __name__ == "__main__": # Start DHT Node on port 60000 dht = DHT(host='0.0.0.0', port=60000) dht.start() while True: continue # Boostrap it #dht.bootstrap('localhost', 6001)
file_handler = logging.FileHandler("HASH.log") file_handler.setFormatter(formatter) logging.getLogger("btdht").setLevel(fileLogLevel) logging.getLogger("btdht").addHandler(file_handler) logging.getLogger("std").setLevel(stdLogLevel) logging.getLogger("std").addHandler(stdout_handler) # thread number threadNumb = THREAD_NUMBER # working time for threads workingTime = WORKINGTIME # threads pool threads = [] for i in xrange(threadNumb): i = i + 9500 thread = DHT(host='0.0.0.0', port=i) thread.start() thread.bootstrap('router.bittorrent.com', 6881) CurrentMagnet = "4CDE5B50A8930315B479931F6872A3DB59575366" thread.ht.add_hash(CurrentMagnet.decode("hex")) threads.append(thread) print "start node %d" % i # time.sleep(2) time.sleep(workingTime) for i in threads: print "stop thread " + i.name i.stop() i.join() # i.rt.saveAllPeer(i.name) # i.ht.saveHashInfo(i.name)
stdout_handler.setFormatter(formatter) logging.getLogger("btdht").setLevel(loglevel) logging.getLogger("btdht").addHandler(stdout_handler) logger = logging.getLogger(__name__) logger.setLevel(loglevel) logger.addHandler(stdout_handler) #thread number threadNumb=5 #working time for threads workingTime=60*60*6 #threads pool threads=[] for i in xrange(threadNumb): print i+9500 #thread = DHT(host='192.168.1.101', port=i) thread = DHT(host='0.0.0.0', port=i) thread.start() threads.append(thread) time.sleep(5) time.sleep(workingTime) for i in threads: print "stop thread "+i.name i.stop() i.join() #i.rt.saveAllPeer(i.name) #i.ht.saveHashInfo(i.name) i.rt.nodes.clear() i.ht.hashes.clear() print 'finish thread'+i.name