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 bytes
Returns:True if the data collection was successful, False if the connection was closed
compression
expected
fileno()[source]

Mirror for the fileno() method of the connection’s underlying socket

find_terminator()[source]

Returns whether the defined return sequences is found

found_terminator()[source]

Processes received messages

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 tuple containing the packets received in this message
Returns:True if an action was taken, False if 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 InternalMessage object
  • *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 IntenalMessage object you just sent, or None if the sending was unsuccessful

send_InternalMessage(msg)[source]

Sends a preconstructed message

Parameters:msg – The IntenalMessage you wish to send
Returns:the IntenalMessage object you just sent, or None if 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 use
  • ValueError – If your peer-to-peer socket is set up with an unknown encryption method
_logger
alive
conn_type
daemon
exceptions
kill_old_nodes(handler)[source]

Cleans out connections which never finish a message

main_thread
process_data(handler)[source]

Collects incoming data from nodes

protocol

Returns server.protocol

server
sock
class py2p.base.BaseSocket(*args, **kwargs)[source]

The base class for a peer-to-peer socket abstractor

Inheritance diagram of py2p.base.BaseSocket
_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.Protocol object
  • 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:
Returns:

True if an action was taken, None if not.

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 a py2p.base.Message object, and handler is a py2p.base.BaseConnection object. It should return True if 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:
id

Returns the message id

msg
packets

Returns a tuple containing the message payload encoded as bytes

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