A Python module designed to simplify the implementation of socket-based server-client communication
Project description
super-easy-socket
super-easy-socket
is a Python module designed to simplify the implementation of socket-based server-client communication. It provides an easy-to-use API for creating servers and clients that can send and receive messages, handle custom functions, and manage connections.
Features
- Easy-to-use server and client classes.
- Support for custom functions associated with specific keywords.
- Automatic ping and pong for connection health monitoring.
- Threaded handling of client connections on the server side.
Installation
You can install super-easy-socket
using pip:
pip install super-easy-socket
Usage
Server Side
The server listens for incoming client connections and can handle custom functions based on keywords.
Example Server (server.py)
from super_easy_socket import Server
# Define custom functions to handle specific keywords
def handle_message(client, payload):
print(f"Received from {client.ip}: {payload}")
if payload == 'hello':
client.send('response', 'Hello, client!')
elif payload == 'bye':
client.send('response', 'Goodbye, client!')
client.close_connection()
# Create a server object
my_server = Server(('127.0.0.1', 8080), 5)
# Associate functions with keywords
my_server.New_Function('message', handle_message)
# Start the server
my_server.start(ping=True)
Client Side
The client connects to a server and can send messages or handle responses based on custom functions.
Example Client (client.py)
from super_easy_socket import Client
# Define custom functions to handle specific keywords from the server
def handle_response(server, payload):
print(f"Received from server: {payload}")
if payload == 'Goodbye, client!':
client.close_connection()
# Define a function to run when connected to the server
def on_connect(client):
client.send('message', 'hello')
# Create a client object
my_client = Client(('127.0.0.1', 8080))
# Associate functions with keywords
my_client.New_Function('response', handle_response)
my_client.on_connect(on_connect)
# Connect to the server
my_client.connect(ping=True)
How to Use
Server Class
Creating a Server
my_server = Server(('127.0.0.1', 8080), 5)
('127.0.0.1', 8080)
: Server IP address and port.5
: Maximum number of clients.
Associating Functions with Keywords
my_server.New_Function('message', handle_message)
'message'
: Keyword that triggers the function.handle_message
: Function to be executed when the keyword is received.
Starting the Server
my_server.start(ping=True)
ping=True
: Enable ping for connection health monitoring.
Client Class
Creating a Client
my_client = Client(('127.0.0.1', 8080))
('127.0.0.1', 8080)
: Server IP address and port.
Associating Functions with Keywords
my_client.New_Function('response', handle_response)
'response'
: Keyword that triggers the function.handle_response
: Function to be executed when the keyword is received.
Setting a Function to Run on Connect
my_client.on_connect(on_connect)
on_connect
: Function to run when the client successfully connects to the server.
Connecting to the Server
my_client.connect(ping=True)
ping=True
: Enable ping for connection health monitoring.
Sending Messages
Messages are sent using the send
method, which associates a keyword with a message payload. The receiving side will use the associated keyword to trigger the corresponding function.
Server Side Sending
To send a message from the server to the client:
client.send('response', 'Hello, client!')
'response'
: The keyword associated with the message.'Hello, client!'
: The message payload.
Client Side Sending
To send a message from the client to the server:
my_client.send('message', 'hello')
'message'
: The keyword associated with the message.'hello'
: The message payload.
License
This module is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file super-easy-socket-0.0.2.tar.gz
.
File metadata
- Download URL: super-easy-socket-0.0.2.tar.gz
- Upload date:
- Size: 2.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bda70a420845942a3c5af520ba3aa02c81bbe4d940ef66c70d0a33ac5aa2d675 |
|
MD5 | 02f55426c4c76445e698087a0101924f |
|
BLAKE2b-256 | 46c6882a1a4c0b50b674b42b022b94363f66903aa510ad7c324f72da010c7d32 |