Utils Module

py2p.utils.log_entry(name, level)[source]

A decorator which allows you to inherit docstrings from a specified function.


Function to sanitize a packet for InternalMessage serialization, or dict keying


Finds the intersection of several iterables

Parameters:*args – Several iterables
Returns:A tuple containing the ordered intersection of all given iterables, where the order is defined by the first iterable


All items in your iterable must be hashable. In other words, they must fit in a set


Retrieves the LAN ip. Expanded from http://stackoverflow.com/a/28950776

Note: This will return ‘’ if it is not connected to a network

py2p.utils.pack_value(l, i)[source]

For value i, pack it into bytes of size length

  • length – A positive, integral value describing how long to make the packed array
  • i – A positive, integral value to pack into said array

A bytes object containing the given value


ValueError – If length is not large enough to contain the value provided


For a string, return the packed value inside of it

Parameters:string – A string or bytes-like object
Returns:An integral value interpreted from this, as if it were a big-endian, unsigned integral

Takes an integer and returns its corresponding base_58 string

Parameters:i – The integral value you wish to encode
Returns:A bytes object which contains the base_58 string
Raises:TypeError – If you feed a non-integral value

Takes a base_58 string and returns its corresponding integer

Parameters:string – The base_58 value you wish to decode (string, bytes, or bytearray)
Returns:Returns integral value which corresponds to the fed string

Returns the current unix time in UTC

Note: This will always return an integral value

py2p.utils.get_socket(protocol, serverside=False)[source]

Given a protocol object, return the appropriate socket

  • protocol – A py2p.base.protocol object
  • serverside – Whether you are the server end of a connection (default: False)

ValueError – If your protocol object has an unknown encryption method


A socket-like object

class py2p.utils.awaiting_value(value='')[source]

Proxy object for an asynchronously retrieved item

callback_method(method, key)[source]

Returns the most common element in a list

Parameters:tmp – A non-string iterable
Returns:The most common element in the iterable


If there are multiple elements which share the same count, it will return a random one.