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.
Implementing py2py takes only 3 lines of code on both the Sender and Listener. Here are very rudimentary examples.
from Sender import Sender s = Sender() s.send('Hello World!','localhost:8080')
from Listener import Listener l = Listener() l.launch(8080)
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'])
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)