A module to connect your server and your clients and exchange information with them
Project description
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
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
pyintercon-2.1.2.tar.gz
(4.8 kB
view hashes)
Built Distribution
Close
Hashes for pyintercon-2.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d9c3c9446dcf88a339fd151d1c5c123bdecdc9e66e310c5e62386e6e76536c39 |
|
MD5 | 15bf60ea8125793b878e1b49b4a5a9d1 |
|
BLAKE2b-256 | 5e3e7d60d48d68977fcadc45e9580abc322fcf732129ee8fa0ff1f4b6b4054db |