Skip to main content

Simple peer2peer library.

Project description


I seek a library that can make a simple P2P network.
This library enables you create P2P application.


  • Asynchronous IO
  • Pure Python code
  • TCP and UDP connection
  • Automatic network build
  • Python3.6+

How to install

warning: Destructive change from 3.0.0

pip3 install --user p2p-python>=3.0.0

How to use

basic usage with debug tool aiomonitor.
install by pip3 install --user aiomonitor.

from p2p_python.utils import setup_p2p_params, setup_logger
from p2p_python.server import Peer2Peer, Peer2PeerCmd
import logging
import asyncio
import aiomonitor
import time
loop = asyncio.get_event_loop()
log = logging.getLogger(__name__)
# setup Peer2Peer
    network_ver=11111,  # (int) identify other network
    p2p_port=2000, # (int) P2P listen port
    p2p_accept=True, # (bool) switch on TCP server
    p2p_udp_accept=True, # (bool) switch on UDP server
p2p = Peer2Peer(listen=100)  # allow 100 connection
# close method example
def close():
    loop.call_later(1.0, loop.stop)
# You can setup DirectDmd method
class DirectCmd(object):
    async def what_is_your_name(user, data):
        print("what_is_your_name", user, data)
        return {"you return": time.time()}
    async def get_time_now(user, data):
        print("get_time_now", user, data)
        return {"get time now": time.time()}
# register methods for DirectCmd
# throw cmd by `await p2p.send_direct_cmd(DirectCmd.what_is_your_name, 'kelly')`
# or `await p2p.send_direct_cmd('what_is_your_name', 'kelly')`
# You can setup broadcast policy (default disabled)
# WARNING: You must set strict policy or will be broken by users
async def broadcast_check_normal(user, data):
    return True
# overwrite method
p2p.broadcast_check = broadcast_check_normal
# setup netcat monitor
local = locals().copy()
local.update({k: v for k, v in globals().items() if not k.startswith('__')})'local', list(local.keys()))
aiomonitor.start_monitor(loop, port=3000, locals=local)"you can connect by `nc 3000`")
# start event loop
# close by `close()` on netcat console
except KeyboardInterrupt:"closing")






Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

p2p_python-3.0.5.tar.gz (24.2 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page