btdht.krcp module¶
-
class
btdht.krcp.BError¶ Bases:
exceptions.ExceptionA base class exception for all bittorrent DHT protocol error exceptions
Parameters: -
e= None¶ A list. The first element is an
intrepresenting the error code. The second element is a string containing the error message
-
t= None¶ string value representing a transaction ID, must be set to the query transaction ID for which an error is raises.
-
y= 'e'¶ The
ykey of the error message. For an error message, it is alwaysb"e"
-
-
class
btdht.krcp.GenericError¶ Bases:
btdht.krcp.BErrorA Generic Error, error code 201
Parameters:
-
class
btdht.krcp.MethodUnknownError¶ Bases:
btdht.krcp.BErrorMethod Unknown, error code 204
Parameters:
-
class
btdht.krcp.ProtocolError¶ Bases:
btdht.krcp.BErrorA Protocol Error, such as a malformed packet, invalid arguments, or bad token, error code 203
Parameters:
-
class
btdht.krcp.ServerError¶ Bases:
btdht.krcp.BErrorA Server Error, error code 202
Parameters:
-
class
btdht.krcp.BMessage¶ Bases:
objectA bittorrent DHT message. This class is able to bdecode a bittorrent DHT message. It expose then the messages keys
t,y,q,errno,errmsgandvas attributes, and behave itself as a dictionnary for theaorrkeys that contains a secondary dictionnary (see Notes).Parameters: - Notes:
A query message is always of the following form with
y == b'q':{ "t": t, "y": y, "q": q, "a": {...} }
A response message is always of the following form with
y == b'r':{ "t": t, "y": y, "r": {...} }
An error message is always in response of a query message and of the following form with
y == b'e':{ "t": t, "y": y, "e":[errno, errmsg] }
The
tkey is a random string generated with every query. It is used to match a response to a particular query.The
ykey is used to differenciate the type of the message. Its value isb'q'for a query,b'r'for a response, andb'e'for and error message.The
qis only present on query message and contain the name of the query (ping, get_peers, announce_peer, find_node)errnoanderrmsgare only defined if the message is an error message. They are respectively the error number (int) and the error describing message of the error.The
vkey is set by some DHT clients to the name and version of the client and is totally optionnal in the protocol.
-
addr¶ The couple (ip, port) source of the message
-
errmsg¶ The error message of the message if the message is and erro message
-
errno¶ The error number of the message if the message is and erro message
-
q¶ The
qkey of the message, should only be define if the message is a query (yis"q"). It countains the name of the RPC method the query is asking for. Can be b’ping’`,b'find_node',b'get_peers',b'announce_peer', …
-
t¶ The
tkey, a random string, transaction id used to match queries and responses together.
-
v¶ The
vkey of the message. This attribute is not describe in the BEP5 that describe the bittorent DHT protocol. It it use as a version flag. Many bittorent client set it to the name and version of the client.
-
y¶ The
y` key of the message. Possible value are ``"q"for a query, “r” for a response and"e"for an error.
-
__getitem__(key)¶ Allow to fetch infos from the secondary dictionnary:
self[b"id"] -> b"..."
Parameters: key (bytes) – The name of an attribute of the secondary dictionnary to retreive. Returns: The value store for keyif foundRaises: KeyError – if keyis not found- Notes:
- Possible keys are:
- id
- target
- info_hash
- token
- nodes
- implied_port
- port
- values
-
__delitem__(key)¶ Allow to unset attributes from the secondary dictionnary:
del self[b'id']
Parameters: bytes key (:param) – The name of an attribute of the secondary dictionnary to unset Returns: Trueifkeyis found and successfully unsetRaises: KeyError – if keyis not found
-
__setitem__(key, value)¶ Allow to set attributes from the secondary dictionnary:
self[b'id'] = b"..."
Parameters: - key (bytes) – The name of an attribute of the secondary dictionnary to set
- value – The value to set
Raises: - KeyError – if
keyis not one of id, target, info_hash, token, nodes, implied_port, port, values. - ValueError – if
valueis not well formated (length, type, …)
-
decode(data, datalen)¶ Bdecode a bencoded message and set the current
BMessageattributes accordinglyParameters: Returns: The remaining of
dataafter the first bencoded message ofdatahas been bdecoded (it may be the empty string ifdatacontains exactly one bencoded message with no garbade at the end).Raises: - DecodeError – If we fail to decode the message
- ProtocolError – If the message is decoded but some attributes are missing of badly formated (length, type, …).
- MissingT – If the message do not have a
b"t"key. Indeed, accordingly to the BEP5, every message (queries, responses, errors) should have ab"t"key.
-
get(key, default=None)¶ Parameters: - key (bytes) – The name of an attribute of the secondary dictionnary to retreive.
- default – Value to return in case
keyis not found. The default isNone
Returns: The value of
keyif found, else the value ofdefault.
-
response(dht)¶ If the message is a query, return the response message to send
Parameters: dht (dht.DHT_BASE) – The dht instance from which the message is originated
Returns: A
BMessageto send as response to the queryRaises: - ProtocolError – if the query is malformated. To send as response to the querier
- MethodUnknownError – If the RPC DHT method asked in the query is unknown. To send as response to the querier