Skip to main content

New in this release:

  • Add callback registry API

  • Add disconnect after 60 seconds of incomplete message

  • Add cryptography support for ssl_wrapper.py

  • Remove net.py

  • Refactor mesh.py

  • Move waterfalling policy to mesh_connection (where it should be)

Usage Guide

Basic Usage

To connect to a mesh network, you will use the mesh_socket object. You can instantiate this as follows:

>>> import py2p
>>> sock = py2p.mesh_socket('0.0.0.0', 4444)

Using '0.0.0.0' will automatically set your outward-facing address as your LAN address. If you want to use an outward-facing internet connection, you will need to specify this address as follows:

>>> import py2p
>>> sock = py2p.mesh_socket('0.0.0.0', 4444, out_addr=('1.2.3.4', 5678))

In addition, you can enable SSL encryption if you have PyOpenSSL or cryptography installed. This works by specifying a custom protocol object, like so:

>>> import py2p
>>> sock = py2p.mesh_socket('0.0.0.0', 4444, prot=py2p.protocol('mesh', 'SSL'))

Eventually that will be the default, but while things are being tested it will default to plaintext. Specifying a different protocol object will ensure that you only can connect to people who share your object structure. So if someone has 'mesh2' instead of 'mesh', you will fail to connect. This check also works against major/minor version numbers. This prevents different version of the underlying protocol from talking with each other. This release supports all versions in 0.3.*.

Unfortunately, this failure is currently silent. Because this is asynchronous in nature, raising an Exception is not possible. Because of this, it’s good to perform the following check after connecting:

>>> import py2p, time
>>> sock = py2p.mesh_socket('0.0.0.0', 4444)
>>> sock.connect('192.168.1.14', 4567)
>>> time.sleep(1)
>>> assert sock.routing_table

To send a message, you should use the send method. Each argument you supply will correspond to a packet that your peer receives. In addition, there are two keyed arguments you can use. flag will specify how other nodes relay this. b'broadcast' will indicate that other nodes are supposed to relay it. b'whisper' will indicate that your peers are not supposed to relay it. There are other technically valid options, but they are not recommended. type will specify what actions other nodes are supposed to take on it. It defaults to b'broadcast', which indicates no change from the norm. There are other valid options, but they should normally be left alone, unless you’ve written a handler (see below) to act on this.

>>> sock.send('this is', 'a test')

Receiving is a bit simpler. When you call the recv method, you receive a message object. This has a number of methods outlined here. Most notably, you can get the packets in a message with message.packets, and reply directly with message.reply().

>>> sock.send('Did you get this?')
>>> msg = sock.recv()
>>> print(msg)
message(type=b'whisper', packets=[b'yes', b'I did'], sender=b'6VnYj9LjoVLTvU3uPhy4nxm6yv2wEvhaRtGHeV9wwFngWGGqKAzuZ8jK6gFuvq737V')
>>> print(msg.packets)
[b'whisper', b'yes', b'I did']
>>> for msg in sock.recv(10):
...     msg.reply("Replying to a list")

Advanced Usage

In addition to this, you can register a custom handler for incoming messages. This is appended to the end of the included ones. When writing your handler, you must keep in mind that you are only passed a message object and a link to the receiving connection. Fortunately you can get access to everything you need from these objects. To see what methods each has, see the API docs. An example service would look like this:

>>> def relay_tx(msg, handler):
...     """Relays bitcoin transactions to various services"""
...     packets = msg.packets  # Gives a list of the non-metadata packets
...     server = msg.server    # Returns your mesh_socket object
...     if packets[0] == b'tx_relay':  # It's important that this flag is bytes
...         from pycoin import tx, services
...         relay = tx.Tx.from_bin(packets[1])
...         services.blockchain_info.send_tx(relay)
...         services.insight.InsightProvider().send_tx(relay)
...         return True        # This tells the daemon to stop calling callbacks
...
>>> import py2p
>>> sock = py2p.mesh_socket('0.0.0.0', 4444)
>>> sock.register_handler(relay_tx)

If this does not take two arguments, register_handler will raise a ValueError. To help debug these services, you can specify a debug_level in the constructor. Using a value of 5, you can see when it enters into each handler, as well as every message which goes in or out.

Release files for py2p 0.3.213

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distributions (sdists)

Source distribution for py2p 0.3.213
File Size Uploaded
py2p-0.3.213.zip 18.0 kB Details
py2p-0.3.213.tar.gz 14.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py2p 0.3.213
File Interpreter ABI Platform
py2p-0.3.213-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size:49.8 kB

Release files / py2p-0.3.213.zip

Download URL py2p-0.3.213.zip
Size 18.0 kB
Tags Source
SHA-256 checksum
How to use checksums
6433fda049728734d2433c2fdcb6cd97297086efa8de509cf6fb0fd768a3353c
BLAKE2b-256 checksum
How to use checksums
58e3a34104f8a1164af261c16a09d6416544f2dfab9d3b847127d62fef54edcf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / py2p-0.3.213.tar.gz

Download URL py2p-0.3.213.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f04eee8f4a38fa349ab416552a7eb784f08ae964cccb5b4f3685c229f473c457
BLAKE2b-256 checksum
How to use checksums
cc37ae7a22387afaa5cfda2687cfc74b564709dea5f2be4b65877cd356ac47dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / py2p-0.3.213-py2.py3-none-any.whl

Download URL py2p-0.3.213-py2.py3-none-any.whl
Size 17.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
5e1f7be048127a39c7ede1799a4940d800a8c8b18581943ccdd580c3fb9513f9
BLAKE2b-256 checksum
How to use checksums
15ee0dcbfb1c028031bee57f55d3db992ed99f05187755baec46f0c7cc989468
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.213 This release

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page