super-easy-socket 0.0.2
A Python module designed to simplify the implementation of socket-based server-client communication
License: MIT
Keywords: super-easy-socket

# 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:

```bash
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)
```python
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)
```python
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
```python
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
```python
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
```python
my_server.start(ping=True)
```
- `ping=True`: Enable ping for connection health monitoring.

### Client Class

#### Creating a Client
```python
my_client = Client(('127.0.0.1', 8080))
```
- `('127.0.0.1', 8080)`: Server IP address and port.

#### Associating Functions with Keywords
```python
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
```python
my_client.on_connect(on_connect)
```
- `on_connect`: Function to run when the client successfully connects to the server.

#### Connecting to the Server
```python
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:
```python
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:
```python
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.