lbry-sdk/scripts/dht_scripts.py

108 lines
2.7 KiB
Python
Raw Normal View History

2017-04-10 16:51:49 +02:00
from lbrynet.core import log_support
import logging.handlers
2015-08-20 17:27:15 +02:00
import sys
2017-04-10 16:51:49 +02:00
import traceback
2015-08-20 17:27:15 +02:00
2017-03-31 20:23:35 +02:00
from lbrynet.dht.node import Node
2017-04-10 16:51:49 +02:00
from twisted.internet import reactor, defer
2017-03-31 20:23:35 +02:00
from lbrynet.core.utils import generate_id
2015-08-20 17:27:15 +02:00
2017-04-10 16:51:49 +02:00
log = logging.getLogger(__name__)
2015-08-20 17:27:15 +02:00
def print_usage():
print "Usage:\n%s UDP_PORT KNOWN_NODE_IP KNOWN_NODE_PORT HASH"
2017-04-10 16:51:49 +02:00
@defer.inlineCallbacks
2015-08-20 17:27:15 +02:00
def join_network(udp_port, known_nodes):
lbryid = generate_id()
2017-04-10 16:51:49 +02:00
log.info('Creating node')
2017-10-10 19:31:07 +02:00
node = Node(udpPort=udp_port, node_id=lbryid)
2015-08-20 17:27:15 +02:00
2017-01-04 23:10:36 +01:00
log.info('Joining network')
2017-04-10 16:51:49 +02:00
yield node.joinNetwork(known_nodes)
2015-08-20 17:27:15 +02:00
2017-04-10 16:51:49 +02:00
defer.returnValue(node)
2015-08-20 17:27:15 +02:00
2017-04-10 16:51:49 +02:00
@defer.inlineCallbacks
2015-08-20 17:27:15 +02:00
def get_hosts(node, h):
log.info("Looking up %s", h)
2017-04-10 16:51:49 +02:00
hosts = yield node.getPeersForBlob(h.decode("hex"))
log.info("Hosts returned from the DHT: %s", hosts)
2015-08-20 17:27:15 +02:00
2017-04-10 16:51:49 +02:00
@defer.inlineCallbacks
2015-08-20 17:27:15 +02:00
def announce_hash(node, h):
2017-04-10 16:51:49 +02:00
results = yield node.announceHaveBlob(h, 34567)
for success, result in results:
if success:
log.info("Succeeded: %s", str(result))
else:
log.info("Failed: %s", str(result.getErrorMessage()))
# def get_args():
# if len(sys.argv) < 5:
# print_usage()
# sys.exit(1)
# udp_port = int(sys.argv[1])
# known_nodes = [(sys.argv[2], int(sys.argv[3]))]
# h = binascii.unhexlify(sys.argv[4])
# return udp_port, known_nodes, h
@defer.inlineCallbacks
def connect(port=None):
try:
if port is None:
raise Exception("need a port")
known_nodes = [('54.236.227.82', 4444)] # lbrynet1
node = yield join_network(port, known_nodes)
log.info("joined")
reactor.callLater(3, find, node)
except Exception:
log.error("CAUGHT EXCEPTION")
traceback.print_exc()
log.info("Stopping reactor")
yield reactor.stop()
@defer.inlineCallbacks
def find(node):
try:
log.info("Approximate number of nodes in DHT: %s", str(node.getApproximateTotalDHTNodes()))
log.info("Approximate number of blobs in DHT: %s", str(node.getApproximateTotalHashes()))
2015-08-20 17:27:15 +02:00
2017-04-10 16:51:49 +02:00
h = "578f5e82da7db97bfe0677826d452cc0c65406a8e986c9caa126af4ecdbf4913daad2f7f5d1fb0ffec17d0bf8f187f5a"
peersFake = yield node.getPeersForBlob(h.decode("hex"))
print peersFake
peers = yield node.getPeersForBlob(h.decode("hex"))
print peers
2015-08-20 17:27:15 +02:00
2017-04-10 16:51:49 +02:00
# yield get_hosts(node, h)
except Exception:
log.error("CAUGHT EXCEPTION")
traceback.print_exc()
2015-08-20 17:27:15 +02:00
2017-04-10 16:51:49 +02:00
log.info("Stopping reactor")
yield reactor.stop()
2015-08-20 17:27:15 +02:00
2017-04-10 16:51:49 +02:00
def main():
log_support.configure_console(level='DEBUG')
log_support.configure_twisted()
reactor.callLater(0, connect, port=10001)
log.info("Running reactor")
2015-08-20 17:27:15 +02:00
reactor.run()
2017-04-10 16:51:49 +02:00
if __name__ == '__main__':
sys.exit(main())