pyintercon

A module to connect your server and your clients and exchange information with them


Keywords
module, python
License
MIT
Install
pip install pyintercon==2.1.2

Documentation

Pyintercon is a python module to connect a server and clients allowing them to exchange information

When an instance of Client sends a request to an instance of Server, the value returned by the request handler function is the response sent to the client.
See below for how to set a request handler function and how it works

Installation

    pip install pyintercon

Example:

Server:

    >>> from pyintercon import Server

    >>> # initialize the server
    >>> sv = Server()

    >>> # run the server using hostname and port
        # the default hostname is "localhost"
    >>> sv.activate("", 8080)
    """Server is activated on localhost:8080...
    Tap CTRL + C to quit !!!!!"""

Client

    >>> from pyintercon import Client

    >>> # initialize the client
    >>> cl = Client()

    >>> # connect client to server by calling the connect method with $hostname and $port used as for server.activate
    >>> cl.connect("localhost", 8080)


    # you will get on server side a message like this
    >>> """ server output:
            ('127.0.0.1', 44042) is connected ...
        """

    >>> # You are ready to go with pyintercon
    >>> # You can send a request to server by calling the send method of the client instance
    >>> response = cl.send({ message: "Hello World !" })

    >>> print(response)
    {'status': 1, 'message': 'default'}
    >>> # use disconnect method to disconnect from the server
    >>> cl.disconnect()

    # you will get on server side a message like this
    >>> """ server output:
            ('127.0.0.1', 44042) is disconnected ...
        """

The server manage the request by executing the request handler function and sends its returned value as response

This function takes the request data (dict object) and returns the a value which will be used as response. The default handler returns {"status": 1, "message": "default"} for everything.
This can be edited by setting a custom handler using setRequestHandler method

   >>> sv = Server()
   >>> server.setRequestHandler(your_handler_function)

See example below

Example:

Server

from pyintercon import Server

def response_loader(request):
    """ Just reverse the message content.
        It takes dict object and returns dict object

    """

    res = {"message": request["message"][::-1]}

    return res

def main():
    sv = Server()

    # set the request handler function
    sv.setRequestHandler(response_loader)

    sv.activate("localhost", 8080)

if __name__ == "__main__":
    main()

Client

    >>> from pyintercon import Client
    >>> cl = Client()
    >>> cl.connect("localhost", 8080)
    >>> res = cl.send({"message": "Hello World !"})
    >>>
    >>> print(res)
    {"message": "! dlroW olleH"}

Features:

  • Add more events, possibility to emit events.
    client.emit("event_name", callback_function)
    at the moment sending the request is considered as the only event