Skip to main content
https://coveralls.io/repos/Storj/pyp2p/badge.svg?branch=develop&service=github https://travis-ci.org/Storj/pyp2p.svg?branch=develop

Welcome to PyP2P

PyP2P is a simplified networking library for building peer-to-peer networks in Python. The library is designed to solve the pain of finding nodes and bypassing NATs so you can focus on writing your application code.

  • Automated port forwarding with UPnP and NATPMP

  • Support for TCP hole punching / simultaneous open

  • Reverse connect (tell a node to connect to you)

  • Fail-safe proxying (planned feature)

  • Python 2 (tested on 2.7 - experimental) & 3 (tested on 3.3)

  • Linux and Windows - yep

Code example

PyP2P is designed to work with simple non-blocking TCP sockets. To use them, your application must create an infinite loop which is used to periodically look for new replies. As you look for replies, the software also handles accepting new connections and removing old ones automatically.

The library also handles constructing replies, which are returned in full as a simple list. The underlying message format is a simple line-based protocol: the messages you want to send are terminated with a new line and are returned in full when they’ve arrived which makes debugging and developing p2p protocols very simple (text-based protocols are easy to debug.)

Alice node

This will create a new listening server on port 44444, bound to listen for connections from the LAN. The interface is specified mostly to ensure that connections are only made from that interface. By default, connections will be made from the default interface (usually wlan0 or eth0) which isn’t useful for simulating and testing a P2P network on the same computer.

from pyp2p.net import *
import time

#Setup Alice's p2p node.
alice = Net(passive_bind="192.168.0.45", passive_port=44444, interface="eth0:2", node_type="passive", debug=1)
alice.start()
alice.bootstrap()
alice.advertise()

#Event loop.
while 1:
    for con in alice:
        for reply in con:
            print(reply)

    time.sleep(1)

Bob node

This code will make a connection to the Alice node and repeatedly send her the word test. Note how they’re both on different interfaces, with completely different IPs. This is necessary for connecting to nodes on the same computer as the library doesn’t allow the Net class to connect to itself itself when running in P2P mode (type=”p2p” for the Net class.) If you want to be able to make duplicate connections to nodes on the same interface then specify the type as “direct” which will make testing code easier. Note that type is “p2p” by default.

from pyp2p.net import *

#Setup Bob's p2p node.
bob = Net(passive_bind="192.168.0.44", passive_port=44445, interface="eth0:1", node_type="passive", debug=1)
bob.start()
bob.bootstrap()
bob.advertise()

#Event loop.
while 1:
    for con in bob:
        con.send_line("test")

    time.sleep(1)

Direct connect

The code shown so far is good for standard broadcast / flooding style P2P networks where the only requirement is to get a message out to the whole network (e.g. Bitcoin and Bitmessage) - but if you want to do anything more complicated you’re going to need to be able to communicate with nodes directly.

Theoretically you can specify the recipient of a message and broadcast it to the network to reach them but this approach won’t scale well for most people. What is needed is a way to direct connect to a node with a high level of reliability. To support this function we use something called a UNL: short for Universal Node Locator.

UNLs describe how to connect to a node behind a NAT, firewall, or on the same LAN by looking at the nodes network information in relation to other nodes and using a variety of subversive techniques including UPnP, NATPMP, and TCP hole punching. To further increase the reliability of this code: the software can also be used with a patched instance of the Kademlia DHT to accept direct messages from other nodes on the DHT that instruct it where to connect back to. This is extremely useful for connecting to nodes behind a NAT as it completely bypasses the need for port forwarding assuming that the source is accessible.

from pyp2p.net import *
from pyp2p.unl import UNL
from pyp2p.dht_msg import DHT
import time


#Start Alice's direct server.
alice_dht = DHT()
alice_direct = Net(passive_bind="192.168.0.45", passive_port=44444, interface="eth0:2", net_type="direct", dht_node=alice_dht, debug=1)
alice_direct.start()

#Start Bob's direct server.
bob_dht = DHT()
bob_direct = Net(passive_bind="192.168.0.44", passive_port=44445, interface="eth0:1", net_type="direct", node_type="active", dht_node=bob_dht, debug=1)
bob_direct.start()

#Callbacks.
def success(con):
    print("Alice successfully connected to Bob.")
    con.send_line("Sup Bob.")

def failure(con):
print("Alice failed to connec to Bob\a")

events = {
    "success": success,
    "failure": failure
}

#Have Alice connect to Bob.
alice_direct.unl.connect(bob_direct.unl.construct(), events)

#Event loop.
while 1:
#Bob get reply.
for con in bob_direct:
    for reply in con:
        print(reply)

#Alice accept con.
for con in alice_direct:
    x = 1

time.sleep(0.5)

In the previous code the Net class was used to spawn a server to accept connections from nodes on the p2p network and managing connections for the purpose of broadcasting. To manage direct connections the same class is used, the difference is the class disables bootstrapping and advertising the connection details to the bootstrapping server as this service is reserved specifically for receiving direct connections.

Dependencies

  • netifaces

  • ntplib

  • twisted

  • ipaddress

  • requests

  • nose

  • setuptools

  • pyroute2

Installation: python3.3 setup.py install

Status: Experimental, may have bugs

Metadata

Release files for pyp2p 0.6.7

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

Source distribution (sdist)

Source distribution for pyp2p 0.6.7
File Size Uploaded Provenance Actions
pyp2p-0.6.7.zip 63.0 kB Without provenance Details for pyp2p-0.6.7.zip

Built distribution (wheel)

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

Total release size: 122.9 kB

Release files / pyp2p-0.6.7.zip

Download URL pyp2p-0.6.7.zip
Size 63.0 kB
Tags Source
SHA-256 checksum
How to use checksums
8681fdcafa46f6613d289bffd7e8b8a0a1ce4fe61dbb539a8df223de0f32998e
BLAKE2b-256 checksum
How to use checksums
c4e8b59ffff3b3adc14f406426a97ced558d8562f4d0d9208c4d35ba92e6a290
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / pyp2p-0.6.7-py2.py3-none-any.whl

Download URL pyp2p-0.6.7-py2.py3-none-any.whl
Size 59.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
bdffe8d1aa53dcb0c1358050703604450905608ae2f9220376a52548a8884688
BLAKE2b-256 checksum
How to use checksums
cf45dba27227f6101310eae737a8c455456491e0c54d8e7266fd90245d7ade9d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for pyp2p 0.6.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.6.7 This release

2 release files No provenance

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