Skip to main content

build

python-p2p

Contribution: this project is open to contributions. Any contribution will be highly apreceated. Feel free to ask any questions related.

A totally decentralized python peer to peer network.

The code provides a simple network structure for information exchange between peers.

Using https://github.com/gryphius/port-forward for port forward by upnp.

Install

note: tested on python 3.9+

Install from pypi:

pip install pythonp2p

Install from source

pip install .

Usage

Import

from pythonp2p import Node

Start

First, you need to initialize the node and then start it. Look at below at Receiving Data to learn how to extend he class first.

node = Node(HOST, PORT, FILE_PORT)  # start the node
node.start()

HOST (OPTIONAL): The host where the socket runs on. Don't touch this if you don't have a weird network config. Default is "".

PORT (OPTIONAL): The port where the nodes communicates. Default 65432

FILE_PORT (OPTIONAL): The port which the server for file transfer is listening on. Default 65433

Connection

node.connect_to(ip)

ip: The other node's ip. After this all other known peers to the other node will be sent to you to connect to. This is automatic.

port (OPTIONAL): The default is the port the node is running on.

Saving state

Save current peers to a file.

node.savestate(file)

Connect to previously discovered peers.

node.loadstate(file)

file (OPTIONAL): filename to save/load state to/from, default: state.json

Communication

To broadcast data to the network you can do:

node.send_message(data, receiver=None)

data: Dict to be sent to all other nodes.

receiver: A string representing the id/public key of the node the message is for. If specified the message will be encrypted and only that node will be able to receive and read it.

To receive messages simply extend the Node class:

from pythonp2p import Node

class Mynode(Node):
  def on_message(message, sender, private):
    # Gets called everytime there is a new message
node = Mynode()
node.start()

message: variable sent from other node.

sender: a string representing the id/public key of the node that snt the message.

private: bool representing if the message was encrypted and meant only for this node or public.

node properties:

Node.id : unique string identifying the node to the network, used to receive private messages.

Files

Node.setfiledir(path) sets the directory in which files downloaded from the net will be stored.

Node.addfile(path) or pythonp2p.files.addfile(path): Adds a file to the node so it can be requested by others.

path: The absolute path of the file in the computer. This function returns the hash which can be used by other nodes to request the file.

Node.requestFile(filehash): Send a request to the network and if the file is available, download it.

filehash: The hash of the file to request in string format. Look above on addfile to get that.

How it works

  • When a node connects no another it will receive a list of active node to connect to.

  • File sharing. When a node requests a file by its hash it will connect to a node that has it and download it. Files can be shared by adding adding them via command. This can be expanded to become like torrents.

  • Peer discovery. Every node gets a list of neighbors when connected.

  • Messages run the entire network, so every node can get every info out there. This is not very good for big networks but it works on a small scale and sending rate can be edited for scale.

  • Public key encryption so private messages can be sent.

  • Message signatures so no node can be impersonated.

  • Nodes ping each other and decide if a node is dead.

  • more idk read the code

Issues

  • mostly security. Do not use this for production, only fiddling around. I am not responsible if you get hacked because of security vulnerabilities here.

Metadata

Release files for pythonp2p 1.7.2

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

Source distribution (sdist)

Source distribution for pythonp2p 1.7.2
File Size Uploaded
pythonp2p-1.7.2.tar.gz 26.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pythonp2p 1.7.2
File Interpreter ABI Platform
pythonp2p-1.7.2-py3-none-any.whl Python 3 none any Details

Total release size: 52.7 kB

Release files / pythonp2p-1.7.2.tar.gz

Download URL pythonp2p-1.7.2.tar.gz
Size 26.4 kB
Tags Source
SHA-256 checksum
How to use checksums
abde4cee64f744366dc482cad5b625234280b8ad237a2b7274e5a80365dd7499
BLAKE2b-256 checksum
How to use checksums
a7962bbd9eb0fb4888d9e38f7db6bb7bdb039d063438c593754c0340ddf05655
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.2

Release files / pythonp2p-1.7.2-py3-none-any.whl

Download URL pythonp2p-1.7.2-py3-none-any.whl
Size 26.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
afee0c90c1fca5161523125f6b10f110cd3565840d475b5707b861615a337b70
BLAKE2b-256 checksum
How to use checksums
5bc28105c6a7b3e28c00ee865e001339f9012f57c62daf4f2f3e89de3a5eceda
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.2

Release history Release notifications | RSS feed

This release

1.7.2 This release

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.1

2 release files

1.5

2 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