Skip to content

dionkaps7/EasySocket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EasySocket 🌐

EasySocket

Welcome to EasySocket, your go-to solution for building a Python socket server with ease. This lightweight framework allows you to integrate socket functionality quickly, making network programming straightforward and accessible.

Table of Contents

  1. Features
  2. Installation
  3. Getting Started
  4. Usage
  5. Examples
  6. API Reference
  7. Contributing
  8. License
  9. Support

Features

  • Effortless Integration: Quickly set up a socket server with minimal configuration.
  • Lightweight: The framework is designed to be fast and efficient, without unnecessary overhead.
  • Callback Support: Easily manage events and responses using callback functions.
  • Minimal Setup: Get started with just a few lines of code.
  • Python 3 Compatibility: Fully compatible with Python 3, ensuring modern programming practices.

Installation

To install EasySocket, simply clone the repository and install the required packages. Use the following commands:

git clone https://github.com/dionkaps7/EasySocket.git
cd EasySocket
pip install -r requirements.txt

For the latest releases, visit the Releases section. Download the latest version and execute the setup.

Getting Started

To get started with EasySocket, follow these simple steps:

  1. Import the EasySocket module in your Python script.
  2. Create an instance of the socket server.
  3. Define your callback functions to handle incoming connections and messages.
  4. Start the server.

Here’s a simple example to illustrate the process.

Usage

Basic Server Setup

from easysocket import EasySocket

def on_connect(client_socket):
    print("Client connected:", client_socket)

def on_message(client_socket, message):
    print("Message received:", message)
    client_socket.send("Message received".encode())

server = EasySocket(port=12345)
server.on_connect = on_connect
server.on_message = on_message
server.start()

In this example, we define two callback functions: on_connect to handle new client connections and on_message to process incoming messages.

Advanced Features

EasySocket supports various advanced features, including:

  • Multi-threading: Handle multiple clients simultaneously.
  • Custom Protocols: Easily define your own message formats.
  • Error Handling: Robust error management to keep your server running smoothly.

Examples

Echo Server

Here’s a simple echo server example:

from easysocket import EasySocket

def on_message(client_socket, message):
    client_socket.send(message)

server = EasySocket(port=12345)
server.on_message = on_message
server.start()

This server will echo back any message it receives.

Chat Server

For a more complex application, consider a chat server that broadcasts messages to all connected clients:

from easysocket import EasySocket

clients = []

def on_connect(client_socket):
    clients.append(client_socket)

def on_message(client_socket, message):
    for client in clients:
        if client != client_socket:
            client.send(message)

server = EasySocket(port=12345)
server.on_connect = on_connect
server.on_message = on_message
server.start()

This chat server keeps track of all connected clients and broadcasts messages to everyone except the sender.

API Reference

EasySocket Class

__init__(port: int)

Initializes the EasySocket server on the specified port.

on_connect(client_socket: socket)

Callback function that is called when a new client connects.

on_message(client_socket: socket, message: str)

Callback function that is called when a message is received from a client.

start()

Starts the socket server.

Contributing

We welcome contributions to EasySocket! If you have suggestions or improvements, please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Make your changes and commit them.
  4. Push your branch and create a pull request.

Please ensure your code follows our coding standards and includes tests where applicable.

License

EasySocket is licensed under the MIT License. See the LICENSE file for more details.

Support

For any questions or issues, please check the Releases section. If you encounter problems, feel free to open an issue on GitHub.

Thank you for using EasySocket! We hope it simplifies your socket programming experience.