Skip to main content

Um gerenciador de portas simples

Project description

CEDRINHO - Simple Port Manager

Version: 0.2.3

A simple Python library for managing TCP ports and handling client-server communications. This project provides an abstraction for simplified port handling and communication, developed as part of a project at Instituto Politécnico de Bragança.

Creator:
Yan Jardim Leal

Table of Contents

Features

  • Simple TCP server setup and client connection handling
  • Asynchronous client management using threads
  • Easy-to-use API for listening on ports and sending data
  • Support for custom callback functions for handling received data
  • Built-in connection management and cleanup

Installation

Install the package using pip:

pip install port-manager

Or install from source:

git clone https://github.com/Yan-Jardim-Leal/cedrinho-simple-router-manager
cd port_manager
pip install .

Usage

Basic Server Setup

from port_manager import PortManager

def handle_data(data, address):
    print(f"Received from {address}: {data}")
    # Process the data here
    return "Fine!" #Returns the message to the client

# Create a PortManager instance
manager = PortManager('localhost', 12345)

# Start listening on the port with a callback function
manager.listenPort(handle_data)

Client Connection

from port_manager import PortManager

# Create a client instance
client = PortManager('localhost', 12345)

# Send data to the server
response = client.callPort("Hello, Server!")
print(response.decode('utf-8'))

Closing Connections

# To stop the server and close all connections
success = manager.closeConnections()
if success:
    print("All connections closed successfully")

API Reference

PortManager Class

__init__(ip: str, port: int)

Initializes a new PortManager instance.

  • ip: The IP address to bind to (e.g., 'localhost' or '0.0.0.0')
  • port: The port number to use

listenPort(function: Callable) -> None

Starts listening on the specified port and handles incoming connections asynchronously.

  • function: A callback function that takes two arguments: data (str) and address (tuple). This function is called whenever data is received from a client.

closeConnections() -> bool

Stops the server and closes all active connections.

  • Returns True if successful, False otherwise.

callPort(data: any) -> any

Connects to the server as a client and sends data, then receives a response.

  • data: The data to send (will be encoded to UTF-8)
  • Returns the response data received from the server

Examples

Echo Server

from port_manager import PortManager

def echo_handler(data, address):
    # Echo back the received data
    response = f"Echo: {data}"
    print(response)

manager = PortManager('0.0.0.0', 8080)
manager.listenPort(echo_handler)

Simple Chat Client

from port_manager import PortManager

client = PortManager('localhost', 8080)

while True:
    message = input("Enter message: ")
    if message.lower() == 'quit':
        break
    response = client.callPort(message)
    print(f"Server response: {response.decode('utf-8')}")

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

port_manager-0.2.3.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

port_manager-0.2.3-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file port_manager-0.2.3.tar.gz.

File metadata

  • Download URL: port_manager-0.2.3.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for port_manager-0.2.3.tar.gz
Algorithm Hash digest
SHA256 2ffd9e470ae08a555fdc5518af2115fb27b300c5b4d6e7d2a1c0c17a167a61d1
MD5 981c93199ace4d3120f052fdb05696ac
BLAKE2b-256 d3f1f05360d78635670f6cd852c2139c1f94ee94ee5797f2ea776299b356a763

See more details on using hashes here.

File details

Details for the file port_manager-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: port_manager-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for port_manager-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4aa42613ddb54e2b463b0d079034b8200ebb55c5758ef5b0b8555d5b71b08a37
MD5 248ecdbd9800b6830dcb3a7a4e227789
BLAKE2b-256 c4717a8bbdff0892458b948762780f35478408891082fa1596fee5960dd4ae4f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page