btdht.krcp module¶
-
class
btdht.krcp.
BError
¶ Bases:
exceptions.Exception
A base class exception for all bittorrent DHT protocol error exceptions
Parameters: -
e
= None¶ A list. The first element is an
int
representing 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
y
key of the error message. For an error message, it is alwaysb"e"
-
-
class
btdht.krcp.
GenericError
¶ Bases:
btdht.krcp.BError
A Generic Error, error code 201
Parameters:
-
class
btdht.krcp.
MethodUnknownError
¶ Bases:
btdht.krcp.BError
Method Unknown, error code 204
Parameters:
-
class
btdht.krcp.
ProtocolError
¶ Bases:
btdht.krcp.BError
A Protocol Error, such as a malformed packet, invalid arguments, or bad token, error code 203
Parameters:
-
class
btdht.krcp.
ServerError
¶ Bases:
btdht.krcp.BError
A Server Error, error code 202
Parameters:
-
class
btdht.krcp.
BMessage
¶ Bases:
object
A bittorrent DHT message. This class is able to bdecode a bittorrent DHT message. It expose then the messages keys
t
,y
,q
,errno
,errmsg
andv
as attributes, and behave itself as a dictionnary for thea
orr
keys 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
t
key is a random string generated with every query. It is used to match a response to a particular query.The
y
key 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
q
is only present on query message and contain the name of the query (ping, get_peers, announce_peer, find_node)errno
anderrmsg
are 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
v
key 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
q
key of the message, should only be define if the message is a query (y
is"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
t
key, a random string, transaction id used to match queries and responses together.
-
v
¶ The
v
key 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 key
if foundRaises: KeyError – if key
is 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: True
ifkey
is found and successfully unsetRaises: KeyError – if key
is 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
key
is not one of id, target, info_hash, token, nodes, implied_port, port, values. - ValueError – if
value
is not well formated (length, type, …)
-
decode
(data, datalen)¶ Bdecode a bencoded message and set the current
BMessage
attributes accordinglyParameters: Returns: The remaining of
data
after the first bencoded message ofdata
has been bdecoded (it may be the empty string ifdata
contains 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
key
is not found. The default isNone
Returns: The value of
key
if 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
BMessage
to 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