**This library is depricated. See https://github.com/robertsdotpm/p2pd for the new version**
#############################################################################################
Welcome to PyP2P |BuildLink|_ |CoverageLink|_ |BuildLink2|_ |CoverageLink2|_ |LicenseLink|_
.. |BuildLink| image:: https://img.shields.io/travis/Storj/pyp2p/master.svg?label=Build-Master
.. _BuildLink: https://travis-ci.org/Storj/pyp2p
.. |CoverageLink| image:: https://img.shields.io/coveralls/Storj/pyp2p/master.svg?label=Coverage-Master
.. _CoverageLink: https://coveralls.io/r/Storj/pyp2p
.. |BuildLink2| image:: https://img.shields.io/travis/Storj/pyp2p/develop.svg?label=Build-Develop
.. _BuildLink2: https://travis-ci.org/Storj/pyp2p
.. |CoverageLink2| image:: https://img.shields.io/coveralls/Storj/pyp2p/develop.svg?label=Coverage-Develop
.. _CoverageLink2: https://coveralls.io/r/Storj/pyp2p
.. |LicenseLink| image:: https://img.shields.io/badge/license-MIT-blue.svg
.. _LicenseLink: https://raw.githubusercontent.com/Storj/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.
.. code:: python
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.
.. code:: python
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.
.. code:: python
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
Raw data
{
"_id": null,
"home_page": "http://github.com/Storj/pyp2p",
"name": "pyp2p",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "NAT traversal,TCP hole punching,simultaneous open,UPnP,NATPMP,P2P,Peer-to-peer networking library,python",
"author": "Storj",
"author_email": "matthew@storj.io",
"download_url": "https://files.pythonhosted.org/packages/3f/36/ca7e456fb6cc350f84af2441e020646943e860963ddc9db5ca11ee034369/pyp2p-0.8.3.tar.gz",
"platform": null,
"description": "**This library is depricated. See https://github.com/robertsdotpm/p2pd for the new version**\r\r\n#############################################################################################\r\r\n\r\r\nWelcome to PyP2P |BuildLink|_ |CoverageLink|_ |BuildLink2|_ |CoverageLink2|_ |LicenseLink|_\r\r\n\r\r\n.. |BuildLink| image:: https://img.shields.io/travis/Storj/pyp2p/master.svg?label=Build-Master\r\r\n.. _BuildLink: https://travis-ci.org/Storj/pyp2p\r\r\n\r\r\n.. |CoverageLink| image:: https://img.shields.io/coveralls/Storj/pyp2p/master.svg?label=Coverage-Master\r\r\n.. _CoverageLink: https://coveralls.io/r/Storj/pyp2p\r\r\n\r\r\n.. |BuildLink2| image:: https://img.shields.io/travis/Storj/pyp2p/develop.svg?label=Build-Develop\r\r\n.. _BuildLink2: https://travis-ci.org/Storj/pyp2p\r\r\n\r\r\n.. |CoverageLink2| image:: https://img.shields.io/coveralls/Storj/pyp2p/develop.svg?label=Coverage-Develop\r\r\n.. _CoverageLink2: https://coveralls.io/r/Storj/pyp2p\r\r\n\r\r\n.. |LicenseLink| image:: https://img.shields.io/badge/license-MIT-blue.svg\r\r\n.. _LicenseLink: https://raw.githubusercontent.com/Storj/pyp2p\r\r\n\r\r\nPyP2P 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.\r\r\n\r\r\n* Automated port forwarding with UPnP and NATPMP\r\r\n* Support for TCP hole punching / simultaneous open\r\r\n* Reverse connect (tell a node to connect to you)\r\r\n* Fail-safe proxying (planned feature)\r\r\n* Python 2 (tested on 2.7 - experimental) & 3 (tested on 3.3)\r\r\n* Linux and Windows - yep\r\r\n\r\r\n============\r\r\nCode example\r\r\n============\r\r\nPyP2P 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.\r\r\n\r\r\nThe 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.)\r\r\n\r\r\n==========\r\r\nAlice node\r\r\n==========\r\r\nThis 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.\r\r\n\r\r\n.. code:: python\r\r\n\r\r\n from pyp2p.net import *\r\r\n import time\r\r\n\r\r\n #Setup Alice's p2p node.\r\r\n alice = Net(passive_bind=\"192.168.0.45\", passive_port=44444, interface=\"eth0:2\", node_type=\"passive\", debug=1)\r\r\n alice.start()\r\r\n alice.bootstrap()\r\r\n alice.advertise()\r\r\n\r\r\n #Event loop.\r\r\n while 1:\r\r\n for con in alice:\r\r\n for reply in con:\r\r\n print(reply)\r\r\n\r\r\n time.sleep(1)\r\r\n\r\r\n========\r\r\nBob node\r\r\n========\r\r\nThis 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.\r\r\n\r\r\n.. code:: python\r\r\n\r\r\n from pyp2p.net import *\r\r\n\r\r\n #Setup Bob's p2p node.\r\r\n bob = Net(passive_bind=\"192.168.0.44\", passive_port=44445, interface=\"eth0:1\", node_type=\"passive\", debug=1)\r\r\n bob.start()\r\r\n bob.bootstrap()\r\r\n bob.advertise()\r\r\n\r\r\n #Event loop.\r\r\n while 1:\r\r\n for con in bob:\r\r\n con.send_line(\"test\")\r\r\n\r\r\n time.sleep(1)\r\r\n\r\r\n==============\r\r\nDirect connect\r\r\n==============\r\r\nThe 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.\r\r\n\r\r\nTheoretically 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.\r\r\n\r\r\nUNLs 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.\r\r\n\r\r\n.. code:: python\r\r\n\r\r\n from pyp2p.net import *\r\r\n from pyp2p.unl import UNL\r\r\n from pyp2p.dht_msg import DHT\r\r\n import time\r\r\n\r\r\n\r\r\n #Start Alice's direct server.\r\r\n alice_dht = DHT()\r\r\n alice_direct = Net(passive_bind=\"192.168.0.45\", passive_port=44444, interface=\"eth0:2\", net_type=\"direct\", dht_node=alice_dht, debug=1)\r\r\n alice_direct.start()\r\r\n\r\r\n #Start Bob's direct server.\r\r\n bob_dht = DHT()\r\r\n 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)\r\r\n bob_direct.start()\r\r\n\r\r\n #Callbacks.\r\r\n def success(con):\r\r\n print(\"Alice successfully connected to Bob.\")\r\r\n con.send_line(\"Sup Bob.\")\r\r\n\r\r\n def failure(con):\r\r\n print(\"Alice failed to connec to Bob\\a\")\r\r\n\r\r\n events = {\r\r\n \"success\": success,\r\r\n \"failure\": failure\r\r\n }\r\r\n\r\r\n #Have Alice connect to Bob.\r\r\n alice_direct.unl.connect(bob_direct.unl.construct(), events)\r\r\n\r\r\n #Event loop.\r\r\n while 1:\r\r\n #Bob get reply.\r\r\n for con in bob_direct:\r\r\n for reply in con:\r\r\n print(reply)\r\r\n\r\r\n #Alice accept con.\r\r\n for con in alice_direct:\r\r\n x = 1\r\r\n\r\r\n time.sleep(0.5)\r\r\n\r\r\n\r\r\nIn 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.\r\r\n\r\r\n============\r\r\nDependencies\r\r\n============\r\r\n* netifaces\r\r\n* ntplib\r\r\n* twisted\r\r\n* ipaddress\r\r\n* requests\r\r\n* nose\r\r\n* setuptools\r\r\n* pyroute2\r\r\n\r\r\nInstallation: python3.3 setup.py install\r\r\n\r\r\nStatus: Experimental, may have bugs\r\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python P2P networking library",
"version": "0.8.3",
"project_urls": {
"Homepage": "http://github.com/Storj/pyp2p"
},
"split_keywords": [
"nat traversal",
"tcp hole punching",
"simultaneous open",
"upnp",
"natpmp",
"p2p",
"peer-to-peer networking library",
"python"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3f36ca7e456fb6cc350f84af2441e020646943e860963ddc9db5ca11ee034369",
"md5": "5016cf511ba695cfc4e71b681aef8d37",
"sha256": "75c8e7078f8f4e3c7621e975f7bd1ca19c72bc7d1416444734ac1d1cc9ede08c"
},
"downloads": -1,
"filename": "pyp2p-0.8.3.tar.gz",
"has_sig": false,
"md5_digest": "5016cf511ba695cfc4e71b681aef8d37",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 60259,
"upload_time": "2023-07-30T06:05:16",
"upload_time_iso_8601": "2023-07-30T06:05:16.099187Z",
"url": "https://files.pythonhosted.org/packages/3f/36/ca7e456fb6cc350f84af2441e020646943e860963ddc9db5ca11ee034369/pyp2p-0.8.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-30 06:05:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Storj",
"github_project": "pyp2p",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"requirements": [],
"test_requirements": [],
"lcname": "pyp2p"
}