Base Module¶
A library to store common functions and protocol definitions
-
class
py2p.base.Protocol[source]¶ Defines service variables so that you can reject connections looking for a different service
-
subnet¶ The subnet flag this Protocol uses
-
encryption¶ The encryption method this Protocol uses
-
id¶ The SHA-256 based ID of this Protocol
-
-
class
py2p.base.BaseConnection(*args, **kwargs)[source]¶ The base class for a connection
-
__init__(*args, **kwargs)¶ Sets up a connection to another peer-to-peer socket
Parameters: - sock – The connected socket object
- server – A reference to your peer-to-peer socket
- outgoing – Whether this connection is outgoing (default: False)
-
active¶
-
addr¶
-
buffer¶
-
collect_incoming_data(data)[source]¶ Collects incoming data
Parameters: data – The most recently received bytesReturns: Trueif the data collection was successful,Falseif the connection was closed
-
compression¶
-
expected¶
-
handle_renegotiate(packets)[source]¶ The handler for connection renegotiations
This is to deal with connection maintenance. For instance, it could be that a compression method fails to decode on the other end, and a node will need to renegotiate which methods it is using. Hence the name of the flag associated with it, “renegotiate”.
Parameters: packets – A tuplecontaining the packets received in this messageReturns: Trueif an action was taken,Falseif not
-
id¶
-
last_sent¶
-
outgoing¶
-
protocol¶ Returns server.protocol
-
send(msg_type, *args, **kargs)[source]¶ Sends a message through its connection.
Parameters: - msg_type – Message type, corresponds to the header in a
InternalMessageobject - *args – A list of bytes-like objects, which correspond to the packets to send to you
- **kargs – There are two available keywords:
- id – The ID this message should appear to be sent from (default: your ID)
- time – The time this message should appear to be sent from (default: now in UTC)
Returns: the
IntenalMessageobject you just sent, orNoneif the sending was unsuccessful- msg_type – Message type, corresponds to the header in a
-
send_InternalMessage(msg)[source]¶ Sends a preconstructed message
Parameters: msg – The IntenalMessageyou wish to sendReturns: the IntenalMessageobject you just sent, orNoneif the sending was unsuccessful
-
server¶
-
sock¶
-
time¶
-
-
class
py2p.base.BaseDaemon(*args, **kwargs)[source]¶ The base class for a daemon
-
__init__(*args, **kwargs)¶ Sets up a daemon process for your peer-to-peer socket
Parameters: - addr – The address you wish to bind to
- port – The port you wish to bind to
- server – A reference to the peer-to-peer socket
Raises: socket.error– The address you wanted is already in useValueError– If your peer-to-peer socket is set up with an unknown encryption method
-
_logger¶
-
alive¶
-
conn_type¶
-
daemon¶
-
exceptions¶
-
main_thread¶
-
protocol¶ Returns server.protocol
-
server¶
-
sock¶
-
-
class
py2p.base.BaseSocket(*args, **kwargs)[source]¶ The base class for a peer-to-peer socket abstractor
-
_BaseSocket__closed¶
-
_BaseSocket__handlers¶
-
__init__(*args, **kwargs)¶ Initializes a peer to peer socket
Parameters: - addr – The address you wish to bind to (ie: “192.168.1.1”)
- port – The port you wish to bind to (ie: 44565)
- prot – The protocol you wish to operate over, defined by a
py2p.base.Protocolobject - out_addr – Your outward facing address. Only needed if you’re connecting over the internet. If you use ‘0.0.0.0’ for the addr argument, this will automatically be set to your LAN address.
- debug_level – The verbosity you want this socket to use when printing event data
Raises: socket.error– The address you wanted could not be bound, or is- otherwise used
-
_logger¶
-
awaiting_ids¶
-
close()[source]¶ If the socket is not closed, close the socket
Raises: RuntimeError– The socket was already closed
-
debug_level¶
-
handle_msg(msg, conn)[source]¶ Decides how to handle various message types, allowing some to be handled automatically
Parameters: - msg – A
py2p.base.Messageobject - conn – A
py2p.base.BaseConnectionobject
Returns: True if an action was taken, None if not.
- msg – A
-
id¶
-
incoming¶ IDs of incoming connections
-
out_addr¶
-
outgoing¶ IDs of outgoing connections
-
protocol¶
-
register_handler(method)[source]¶ Register a handler for incoming method.
Parameters: method – A function with two given arguments. Its signature should be of the form handler(msg, handler), where msg is apy2p.base.Messageobject, and handler is apy2p.base.BaseConnectionobject. It should returnTrueif it performed an action, to reduce the number of handlers checked.Raises: ValueError– If the method signature doesn’t parse correctly
-
routing_table¶
-
status¶ The status of the socket.
Returns: "Nominal"if all is going well, or a list of unexpected (Exception, traceback) tuples if not
-
-
class
py2p.base.Message(msg, server)[source]¶ An object which gets returned to a user, containing all necessary information to parse and reply to a message
-
__init__(msg, server)[source]¶ Initializes a Message object
Parameters: - msg – A
py2p.base.InternalMessageobject - server – A
py2p.base.BaseSocketobject
- msg – A
-
id¶ Returns the message id
-
msg¶
-
reply(*args)[source]¶ Replies to the sender if you’re directly connected. Tries to make a connection otherwise
Parameters: *args – Each argument given is a packet you wish to send. This is prefixed with base.flags.whisper, so the other end will receive [base.flags.whisper, *args]
-
sender¶ The ID of this Message’s sender
-
server¶
-
time¶ The time this Message was sent at
-
time_58¶ Returns this message’s timestamp in base_58
-