kademlia package¶
The best place to start is the examples folder before diving into the API.
kademlia.crawling module¶
- class kademlia.crawling.NodeSpiderCrawl(protocol, node, peers, ksize, alpha)[source]¶
Bases: kademlia.crawling.SpiderCrawl
Create a new C{SpiderCrawl}er.
Parameters: - protocol – A KademliaProtocol instance.
- node – A Node representing the key we’re looking for
- peers – A list of Node instances that provide the entry point for the network
- ksize – The value for k based on the paper
- alpha – The value for alpha based on the paper
- class kademlia.crawling.RPCFindResponse(response)[source]¶
Bases: object
A wrapper for the result of a RPC find.
Parameters: response – This will be a tuple of (<response received>, <value>) where <value> will be a list of tuples if not found or a dictionary of {‘value’: v} where v is the value desired
- class kademlia.crawling.SpiderCrawl(protocol, node, peers, ksize, alpha)[source]¶
Bases: object
Crawl the network and look for given 160-bit keys.
Create a new C{SpiderCrawl}er.
Parameters: - protocol – A KademliaProtocol instance.
- node – A Node representing the key we’re looking for
- peers – A list of Node instances that provide the entry point for the network
- ksize – The value for k based on the paper
- alpha – The value for alpha based on the paper
kademlia.log module¶
kademlia.network module¶
Package for interacting on the network at a high level.
- class kademlia.network.Server(ksize=20, alpha=3, id=None, storage=None)[source]¶
Bases: object
High level view of a node instance. This is the object that should be created to start listening as an active node on the network.
Create a server instance. This will start listening on the given port.
Parameters: - ksize (int) – The k parameter from the paper
- alpha (int) – The alpha parameter from the paper
- id – The id for this node on the network.
- storage – An instance that implements IStorage
- bootstrap(addrs)[source]¶
Bootstrap the server by connecting to other known nodes in the network.
Parameters: addrs – A list of (ip, port) tuple pairs. Note that only IP addresses are acceptable - hostnames will cause an error.
- bootstrappableNeighbors()[source]¶
Get a list of (ip, port) tuple pairs suitable for use as an argument to the bootstrap method.
The server should have been bootstrapped already - this is just a utility for getting some neighbors and then storing them if this server is going down for a while. When it comes back up, the list of nodes can be used to bootstrap.
- inetVisibleIP()[source]¶
Get the internet visible IP’s of this node as other nodes see it.
Returns: A list of IP’s. If no one can be contacted, then the list will be empty.
- listen(port)[source]¶
Start listening on the given port.
This is the same as calling:
reactor.listenUDP(port, server.protocol)
- classmethod loadState(fname)[source]¶
Load the state of this node (the alpha/ksize/id/immediate neighbors) from a cache file with the given fname.
- refreshTable()[source]¶
Refresh buckets that haven’t had any lookups in the last hour (per section 2.3 of the paper).
- saveState(fname)[source]¶
Save the state of this node (the alpha/ksize/id/immediate neighbors) to a cache file with the given fname.
kademlia.node module¶
- class kademlia.node.NodeHeap(node, maxsize)[source]¶
Bases: object
A heap of nodes ordered by distance to a given node.
Constructor.
@param node: The node to measure all distnaces from. @param maxsize: The maximum size that this heap can grow to.
- remove(peerIDs)[source]¶
Remove a list of peer ids from this heap. Note that while this heap retains a constant visible size (based on the iterator), it’s actual size may be quite a bit larger than what’s exposed. Therefore, removal of nodes may not change the visible size as previously added nodes suddenly become visible.
kademlia.protocol module¶
- class kademlia.protocol.KademliaProtocol(sourceNode, storage, ksize)[source]¶
Bases: rpcudp.protocol.RPCProtocol
- handleCallResponse(result, node)[source]¶
If we get a response, add the node to the routing table. If we get no response, make sure it’s removed from the routing table.
- transferKeyValues(node)[source]¶
Given a new node, send it all the keys/values it should be storing.
@param node: A new node that just joined (or that we just found out about).
Process: For each key in storage, get k closest nodes. If newnode is closer than the furtherst in that list, and the node for this server is closer than the closest in that list, then store the key/value on the new node (per section 2.5 of the paper)
kademlia.routing module¶
- class kademlia.routing.KBucket(rangeLower, rangeUpper, ksize)[source]¶
Bases: object
kademlia.storage module¶
- class kademlia.storage.ForgetfulStorage(ttl=604800)[source]¶
Bases: object
By default, max age is a week.
kademlia.utils module¶
General catchall for functions that don’t make sense as methods.
- kademlia.utils.deferredDict(d)[source]¶
Just like a C{defer.DeferredList} but instead accepts and returns a C{dict}.
@param d: A C{dict} whose values are all C{Deferred} objects.
@return: A C{DeferredList} whose callback will be given a dictionary whose keys are the same as the parameter C{d}’s and whose values are the results of each individual deferred call.
Find the shared prefix between the strings.
For instance, sharedPrefix([‘blahblah’, ‘blahwhat’]) is ‘blah’.