Skip to main content

A generic asyncio Python package to use telnet.

Project description

aiotelnet: Async Telnet Client

aiotelnet is a lightweight, asynchronous Telnet client for Python, designed for easy integration into modern async applications.

Notice: AI has been used to generate parts of this project. It has been checked against Python documentation for correctness.

This project was born due to the lack of standard async telnet library, while it has been built primarily for telnet usage, it could also be used for any kind of low-level server/client network communication.

Features

  • Asynchronous: Built on asyncio for non-blocking network I/O.
  • Automatic Reconnection: Can automatically reconnect to the server if the connection is lost.
  • Easy to Use: Simple and intuitive API for connecting, sending commands, and handling messages.
  • Customizable: Allows customization of encoding, line breaks, and reconnection behavior.

Installation

pip install aiotelnet

Usage

Here's a basic example of how to use TelnetClient to connect to a Telnet server, send a command, and receive messages:

import asyncio

from aiotelnet.client import TelnetClient


# Define a message handler
async def message_handler(message: bytes):
    """Handle incoming messages from the Telnet server."""
    print(f"Received: {message.decode('utf-8').strip()}")


async def main():
    """Connect to the Telnet server and send a command."""
    # Create a Telnet client
    client = TelnetClient(
        host="telehack.com",
        port=23,
        message_handler=message_handler,
    )

    try:
        # Establish the connection
        await client.connect()

        # Send a command
        await client.send_command("hello")

        # Keep the connection open for a while
        await asyncio.sleep(10)

    except ConnectionError as e:
        print(f"Connection failed: {e}")
    finally:
        # Close the connection
        if client.is_connected():
            await client.close()


if __name__ == "__main__":
    asyncio.run(main())

API Reference

TelnetClient(host, port, message_handler, ...)

  • host: The Telnet server host.
  • port: The Telnet server port (default: 23).
  • message_handler: An optional callable (sync or async) to handle incoming messages.
  • break_line: The byte sequence that separates messages (default: b'\\n').
  • encoding: The encoding to use for commands (default: 'utf-8').
  • auto_reconnect: Whether to automatically reconnect if the connection is lost (default: True).
  • reconnect_interval: The interval in seconds between reconnection attempts (default: 10).

async connect()

Establishes a connection to the Telnet server.

is_connected() -> bool

Returns True if the client is connected, False otherwise.

async send_command(command: str)

Sends a command to the Telnet server.

async close()

Closes the connection to the Telnet server.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the Apache 2.0 License.

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

aiotelnet-1.0.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

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

aiotelnet-1.0.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file aiotelnet-1.0.0.tar.gz.

File metadata

  • Download URL: aiotelnet-1.0.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aiotelnet-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4953ba0f1188292bb0d4ba63a151328b40da17b1a368cda7ac0cb426ff45bcc6
MD5 fb8fb803b5dff8179328f531c271d196
BLAKE2b-256 4ccfcff783b8dc5f1907f1fd2b2bd4a78cdb0ff898db34cb5345631206aed08f

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiotelnet-1.0.0.tar.gz:

Publisher: release.yml on pantherale0/aiotelnet

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file aiotelnet-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: aiotelnet-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aiotelnet-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1942a0472c4efd2a3206dde02cc9f6252844205dec4ce0890689f2ea1496446c
MD5 5da550292726eea77322b37826417d9e
BLAKE2b-256 266be58d1748e14ef1db5b2ec146e6646c6c573eba97b2990934849ac1b3d9eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiotelnet-1.0.0-py3-none-any.whl:

Publisher: release.yml on pantherale0/aiotelnet

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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