def test_successful_get_peers(self): ts, datagrams = self.controller.main_loop() ping_timeout_ts = ts #FIXME: assert_almost_equal(ts, time.time()+2) ping = datagrams[0].data addr = datagrams[0].addr #fabricate response ping = self.servers_msg_f.incoming_msg(Datagram(ping, addr)) pong = self.servers_msg_f.outgoing_ping_response(tc.CLIENT_NODE) data = pong.stamp(ping.tid) # get a node in the routing table self.controller.on_datagram_received(message.Datagram(data, addr)) #The lookup starts with a single node lookup_result = [] datagrams = self.controller.get_peers(lookup_result, tc.INFO_HASH, lambda x, y: x.append(y), 0) #FIXME: assert_almost_equal(ts, ping_timeout_ts)#time.time()+2) #FIXME: eq_(len(datagrams), 1) # Now a get_peers with local results info_hash = identifier.Id('info_hash info_hash ') self.controller._tracker.put(info_hash, tc.CLIENT_ADDR) lookup_result = [] self.controller.get_peers(lookup_result, info_hash, lambda x, y: x.append(y), 0)
def main(options, args): my_addr = (options.ip, options.port) logs_path = options.path logging_conf.setup(logs_path, logs_level) print 'Using the following plug-ins:' print '*', options.routing_m_file print '*', options.lookup_m_file routing_m_mod = __import__(options.routing_m_file[:-3]) lookup_m_mod = __import__(options.lookup_m_file[:-3]) dht = kadtracker.KadTracker(my_addr, logs_path, routing_m_mod, lookup_m_mod) try: info_hashes = open(options.infohash_file) except AttributeError: info_hashes = [] print 'WARNING: no info_hashes file provided' try: print 'Warming up...', sys.stdout.flush() time.sleep(60) print 'DONE' print 'Type Control-C to exit.' for line in info_hashes: info_hash = identifier.Id(line.strip()) print '%s Getting peers for info_hash %r' % ( time.asctime(), info_hash) dht.get_peers(info_hash, _on_peers_found) global start_ts start_ts = time.time() time.sleep(options.lookup_interval) except (KeyboardInterrupt): pass dht.stop() print '\nStopping DHT...', sys.stdout.flush() time.sleep(1) print 'DONE'
def peers_found(peers): print 'Peers found:', time.time() return for peer in peers: print peer print '-' * 20 def lookup_done(): print 'Lookup DONE' info_hashes = ( identifier.RandomId(), identifier.Id('28f2e5ea2bf87eae4bcd5e3fc9021844c01a4df9'), identifier.RandomId(), identifier.Id('dd5c25b4b8230e108fbf9d07f87a86c6b05c9b6d'), identifier.RandomId(), identifier.Id('bcbdb9c2e7b49c65c9057431b492cb7957c8a330'), identifier.RandomId(), identifier.Id('d93df7a507f3c9d2ebfbe49762a217ab318825bd'), identifier.RandomId(), identifier.Id('6807e5d151e2ac7ae92eabb76ddaf4237e4abb60'), identifier.RandomId(), identifier.Id('83c7b3b7d36da4df289670592be68f9dc7c7096e'), identifier.RandomId(), identifier.Id('9b16aecf952597f9bb051fecb7a0d8475d060fa0'), identifier.RandomId(), identifier.Id('24f2446365d3ef782ec16ad63aea1206df4b8d21'), identifier.RandomId(),
import logging, logging_conf logs_path = 'test_logs' logs_level = logging.DEBUG logging_conf.setup(logs_path, logs_level) ''' import identifier from identifier import Id, ID_SIZE_BITS, BITS_PER_BYTE import node TASK_INTERVAL = .01 TIMEOUT_DELAY = .4 CLIENT_ID = identifier.Id('\x41' * identifier.ID_SIZE_BYTES) CLIENT_PORT = 6000 CLIENT_ADDR = ('127.0.0.1', CLIENT_PORT) CLIENT_NODE = node.Node(CLIENT_ADDR, CLIENT_ID) BT_PORT = 7777 SERVER_ID = identifier.Id('\x01' * identifier.ID_SIZE_BYTES) SERVER_PORT = 6001 SERVER_ADDR = ('127.0.0.1', SERVER_PORT) SERVER_NODE = node.Node(SERVER_ADDR, SERVER_ID) SERVER2_ID = identifier.Id('\x43' * identifier.ID_SIZE_BYTES) SERVER2_ADDR = ('128.0.0.2', 6002) SERVER2_NODE = node.Node(SERVER2_ADDR, SERVER2_ID) EXTERNAL_NODE_ADDR = ('128.0.0.1', 6881)
def peers_found(peers): print 'Peers found:', time.time() return for peer in peers: print peer print '-' * 20 def lookup_done(): print 'Lookup DONE' info_hashes = ( identifier.Id('4d2d7ed8f5f2232b8396279d5367bb0b491683de'), identifier.Id('d592fa0e3fd374ab7b98538a25e52e9055df38d9'), identifier.Id('01732053aabe060b553d7e3350857f4f6e4b3fbc'), identifier.Id('9d4bd2a6e80ec6e975c52e5c69e1d339b57f013b'), identifier.Id('30d1bf0c6ba6ab1b3450c888aa3659515337e9a1'), identifier.Id('0bfcc7aba1e69cc24ca6f3ddcf4ad9efada95fd5'), identifier.Id('0b133191e4bdf7f71bed49840fa9fd91087d81a2'), identifier.Id('8556f70ee3a9b8aa3acfaef9aa1df363bde4715a'), identifier.Id('018f8ad955ed98bdf44cca9e5219e18731a785e6'), identifier.Id('12eae0a254858a519c9bc4966b707c0d6f5dc799'), identifier.Id('b3187b9c6eb24aaffb44f5a78b1b7b42eb2ef3ad'), identifier.Id('0f0c33d402742739fd8ee5840b54addc3e315449'), identifier.Id('33ef17223ec069e77958f89b6675a2f927b7c1cc'), identifier.Id('831c6c25e5251dcb1d7898a18a0619d74bdb7268'), identifier.Id('93c27dab6620f45128c4598915c3494ce3eb5d16'), identifier.Id('c36fd75728bbcc6a9cb446821d95756aa7487e9a'),
def lookup_done(): print 'Lookup DONE' print 'Type an info_hash (in hex digits): ', if len(sys.argv) == 4 and sys.argv[0] == 'interactive_dht.py': RUN_DHT = True my_addr = (sys.argv[1], int(sys.argv[2])) #('192.16.125.242', 7000) logs_path = sys.argv[3] dht = kadtracker.KadTracker(my_addr, logs_path) else: RUN_DHT = False print 'usage: python interactive_dht.py dht_ip dht_port log_path' print 'Type "exit" to stop the DHT and exit' while (RUN_DHT): print 'Type an info_hash (in hex digits): ', input = sys.stdin.readline()[:-1] if input == 'exit': dht.stop() break try: info_hash = identifier.Id(input) except (identifier.IdError): print 'Invalid input (%s)' % input continue print 'Getting peers for info_hash %r' % info_hash dht.get_peers(info_hash, peers_found)
def lookup_done(): print 'No peers found' print 'Type "EXIT" to stop the DHT and exit' print 'Type an info_hash (in hex digits):' if len(sys.argv) == 5 and sys.argv[1] == 'interactive_dht': log.critical('argv %r' % sys.argv) assert 0 RUN_DHT = True my_addr = (sys.argv[1], sys.argv[2]) #('192.16.125.242', 7000) logs_path = sys.argv[3] dht = kadtracker.KadTracker(my_addr, logs_path) else: RUN_DHT = False print 'usage: python interactive_dht ip port paht' while (RUN_DHT): input = sys.stdin.readline()[-1] if input == 'EXIT': dht.stop() break try: info_hash = identifier.Id(hex_id) except (IdError): print 'Invalid info_hash (%s)' % hex_id continue dht.get_peers(info_hash, do_nothing)