Skip to main content

A peer to peer network able to transfer data and files

Project description

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.

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

pythonp2p-1.7.2.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pythonp2p-1.7.2-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

Details for the file pythonp2p-1.7.2.tar.gz.

File metadata

  • Download URL: pythonp2p-1.7.2.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for pythonp2p-1.7.2.tar.gz
Algorithm Hash digest
SHA256 abde4cee64f744366dc482cad5b625234280b8ad237a2b7274e5a80365dd7499
MD5 5188e037216d3016ffe92f949fe6f6d0
BLAKE2b-256 a7962bbd9eb0fb4888d9e38f7db6bb7bdb039d063438c593754c0340ddf05655

See more details on using hashes here.

File details

Details for the file pythonp2p-1.7.2-py3-none-any.whl.

File metadata

  • Download URL: pythonp2p-1.7.2-py3-none-any.whl
  • Upload date:
  • Size: 26.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for pythonp2p-1.7.2-py3-none-any.whl
Algorithm Hash digest
SHA256 afee0c90c1fca5161523125f6b10f110cd3565840d475b5707b861615a337b70
MD5 91aa6bbb4ce922f75ab4e4cee2aa09f9
BLAKE2b-256 5bc28105c6a7b3e28c00ee865e001339f9012f57c62daf4f2f3e89de3a5eceda

See more details on using hashes here.

Supported by

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