Skip to main content

p2p-python

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

Specification

  • 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_logger(logging.INFO)
 
# setup Peer2Peer
setup_p2p_params(
    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
p2p.setup()
 
# close method example
def close():
    p2p.close()
    loop.call_later(1.0, loop.stop)
 
# You can setup DirectDmd method
class DirectCmd(object):
 
    @staticmethod
    async def what_is_your_name(user, data):
        print("what_is_your_name", user, data)
        return {"you return": time.time()}
 
    @staticmethod
    async def get_time_now(user, data):
        print("get_time_now", user, data)
        return {"get time now": time.time()}
 
# register methods for DirectCmd
p2p.event.setup_events_from_class(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('__')})
log.info('local', list(local.keys()))
aiomonitor.start_monitor(loop, port=3000, locals=local)
log.info(f"you can connect by `nc 127.0.0.1 3000`")
 
# start event loop
# close by `close()` on netcat console
try:
    loop.run_forever()
except KeyboardInterrupt:
    log.info("closing")
loop.close()

Documents

Author

@namuyan_mine

Licence

MIT

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 details)

Uploaded Source

File details

Details for the file p2p_python-3.0.5.tar.gz.

File metadata

  • Download URL: p2p_python-3.0.5.tar.gz
  • Upload date:
  • Size: 24.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for p2p_python-3.0.5.tar.gz
Algorithm Hash digest
SHA256 93a865fb9e24b30aeb78905c7afde00d27f906feaafdc90773eee19256fd7ca7
MD5 e812a6954615d11340376c0e0fe8aeb9
BLAKE2b-256 aa7dfe57e8083edb92919654778adf2d2c76bbd8845315998f8cb84741f31577

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

3.0.5 This release

1 file

3.0.4

1 file

3.0.3

1 file

3.0.2

1 file

3.0.1

1 file

2.1.1

1 file

2.1.0

1 file

2.0.2

1 file

2.0.1

1 file

2.0.0

1 file

1.1.2

1 file

1.1.1

1 file

1.1.0

1 file

1.0.21

1 file

1.0.19

1 file

1.0.17

1 file

1.0.16

1 file

1.0.14

1 file

1.0.13

1 file

1.0.12

1 file

1.0.10

1 file

1.0.9

1 file

1.0.6

1 file

1.0.5

1 file

1.0.4

1 file

0.0.5

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page