R-Balancer is a simple load balancer implementation using the Round-Robin algorithm. It distributes incoming client requests across multiple backend servers to balance the load. This script is designed for use in Python 3.x.
- Round-Robin Load Balancing: Distributes incoming requests evenly across a list of backend servers.
- Multithreading: Handles multiple client connections simultaneously using threads.
- Configurable: Allows customization of server addresses and ports via command-line arguments.
- Python 3.x
socket
andthreading
modules (built-in Python libraries)
Clone the repository or download the script:
git clone <repository-url>
cd <repository-directory>
Run the script using the following command format:
python R-Balancer.py -s <host> -p <port> -l "<server1>:<port1>,<server2>:<port2>,..."
-s
or--server
: The IP address or hostname where the load balancer will listen for incoming connections.-p
or--port
: The port on which the load balancer will listen.-l
or--list
: A comma-separated list of backend servers in the formathost:port
.
To start the load balancer on 0.0.0.0
port 8080
and distribute requests to backend servers 127.0.0.1:8081
, 127.0.0.1:8082
, and 127.0.0.1:8083
:
python R-Balancer.py -s 0.0.0.0 -p 8080 -l "127.0.0.1:8081,127.0.0.1:8082,127.0.0.1:8083"
Create config file in same directory with name R-Balancer.conf
.
Example config:
{
"server": "0.0.0.0",
"port": 80,
"list_server": "192.168.1.3:80,192.168.1.4:8080,192.168.1.5:8000"
}
- Initialization:
RBalancer
class is initialized with a list of backend servers. - Server List: The
listServer
method parses the backend server list from a string. - Client Handling: The
handle_client
method handles incoming client connections and forwards them to backend servers in a round-robin manner. - Data Forwarding: The
forward_data
method transfers data between client and server. - Starting: The
start
method binds the load balancer to the specified IP and port and begins listening for incoming connections.
This project is licensed under the MIT License. See the LICENSE file for details.