py2py

Lightweight P2P in Python 2.7+ and Python 3 via HTTP POST


Keywords
p2p, peer, lightweight, http, post
License
MIT
Install
pip install py2py==0.2.0

Documentation

py2py

PyPI version Code Climate

A lightweight module for Python 2 and Python 3 which takes all of the fuss out of peer to peer communication. It achieves this via two classes: Sender and Listener. Communication occurs between the two using HTTP POST.

Usage

Implementing py2py takes only 3 lines of code on both the Sender and Listener. Here are very rudimentary examples.

Sender

from Sender import Sender

s = Sender()
s.send('Hello World!','localhost:8080')

Listener

from Listener import Listener

l = Listener()
l.launch(8080)

Event Handler

By default, py2py's Listener class prints the message it receives only if it has no event_handler. You can attach a custom event_handler to the Listener object. The Listener object will attempt to call event_handler.handle_message(msg) if it exists. An example event handler is as follows.

class BasicEventHandler:
  '''Rudimentary Event Handler for use in py2py/readme.md'''

  def handle_message(self, message):
    '''Receives a message from a py2py Listener'''
    print(message['message'])

Examples

PyToPyChat: P2P Chat

Rudimentary plaintext p2p chat client that runs in the Python Interpreter. Usage as follows.

from Examples.PyToPyChat import PyToPyChat

p = PyToPyChat()
p.add_peer('SomeRemoteIp:8080')
p.launch(port=8080)

View Source Code for PyToPyChat