Skip to main content

A simple Python module for creating a TCP chat connection between two computers to send commands or information.

Project description

TcpChat

TcpChat is a simple Python module for creating a TCP chat connection between two computers to send commands or information. GitHub, PyPi

Installation

Use the package manager pip to install TcpChat.

pip install ct-tcpchat

Simple usage

from ct_tcpchat import TcpChat
import logging

def callback_function(message):
    print(f"Received message: {message}")

connect_to_ip = "192.168.1.50" # Enter the IP Address from the other computer
on_port = 44785 # Choose a port for the connection. (Optional. Default = 44785)

connection = TcpChat(connect_to_ip, callback_function, on_port, log_level=logging.INFO)
connection.start()

# Example of sending a message from the main thread
connection.send_message("Hello from server")

Overview

This library provides two classes for TCP communication: TcpChat and TcpChatNoThread. Each class serves different purposes based on the need for non-blocking or blocking operations.

TcpChat (Non-blocking using threading)

The TcpChat class is designed for non-blocking TCP communication by leveraging Python's threading capabilities. This class allows for concurrent handling of server and client connections, ensuring that the main application remains responsive and can handle other tasks simultaneously.

Key Features:

  • Threading for Concurrency: The server and client operations run on separate threads, preventing the main application loop from being blocked.
  • Automatic Connection Management: Automatically handles retries for client connections and accepts incoming connections on the server.
  • Callback Mechanism: Processes incoming messages using a user-defined callback function.
  • Logging Support: Includes logging to monitor connection status, message transfers, and errors.

Usage Example:

from ct_tcpchat import TcpChat
import logging
import time

def callback_function(message):
    print(f"Received message: {message}")

connect_to_ip = "192.168.1.50"
port = 44785
log_level = logging.DEBUG

connection = TcpChat(connect_to_ip=connect_to_ip, callback=callback_function, port=port, log_level=log_level)
connection.retry_connection_interval = 2

if __name__ == "__main__":
    try:
        connection.start()
        while not connection.connected_event.is_set():
            time.sleep(1)
        while connection.connected_event.is_set():
            message = input("Message: ")
            if message:
                connection.send_message(message)
    except KeyboardInterrupt:
        print("\nExiting program...")
        connection.close_connections()

TcpChatNoThread (Blocking without threading)

The 'TcpChatNoThread' class is designed for blocking TCP communication without using threading. This class is suitable for applications where the user wants to control the main loop and manage threading themselves or where the simplicity of blocking operations is desired.

Key Features:

  • Single-threaded Operation: Does not use threads, meaning that server and client operations will block the main loop until they complete.
  • User-managed Loop: Provides a process_events method that the user can call within their main loop to handle incoming connections and messages.
  • Callback Mechanism: Processes incoming messages using a user-defined callback function.
  • Logging Support: Includes logging to monitor connection status, message transfers, and errors.

Usage Example:

from ct_tcpchat_no_thread import TcpChatNoThread
import logging
import time

def callback_function(message):
    print(f"Received message: {message}")

connect_to_ip = "192.168.1.50"
port = 44785
log_level = logging.DEBUG

connection = TcpChatNoThread(connect_to_ip=connect_to_ip, callback=callback_function, port=port, log_level=log_level)
connection.retry_connection_interval = 2

if __name__ == "__main__":
    try:
        connection.start()
        while True:
            message = input("Message: ")
            if message:
                connection.send_message(message)
            connection.process_events()
    except KeyboardInterrupt:
        print("\nExiting program...")
        connection.close_connections()

Key Differences:

  1. Threading:

    • TcpChat uses threading, making it non-blocking and suitable for applications requiring concurrency.
    • TcpChatNoThread does not use threading, making it blocking and simpler but requiring the user to manage the main loop.
  2. Main Loop Management:

    • TcpChat automatically manages its own threads for server and client operations.
    • TcpChatNoThread provides methods to be called in the user's main loop, giving the user more control over the execution flow.
  3. Use Case Suitability:

    • TcpChat is ideal for applications needing responsiveness and concurrency, such as GUIs or real-time systems.
    • TcpChatNoThread is ideal for simpler applications or where the user prefers to manage threading themselves, such as in command-line tools or basic scripts.

Extra functions

TcpChat.get_local_ip() # Returns own IP Address

TcpChat.close_connections() # Closes all the connections

TcpChat.retry_connection_interval = 2 # Set the retry interval (Default = 5 seconds)
License

MIT

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

ct_tcpchat-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

ct_tcpchat-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file ct_tcpchat-0.1.0.tar.gz.

File metadata

  • Download URL: ct_tcpchat-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for ct_tcpchat-0.1.0.tar.gz
Algorithm Hash digest
SHA256 573762b470a9d8da42415ae15add2ae7d791d74be2299a9416c4ae707e5672cf
MD5 ce424b5e9002bc01955d305c8d3771c0
BLAKE2b-256 c5ebf7f47cc3ac27aa281a4e5bc26226d18c41341b616ff0bffdd731c3aedac8

See more details on using hashes here.

File details

Details for the file ct_tcpchat-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ct_tcpchat-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for ct_tcpchat-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0be6aa3091faa0ac5970686ab4108ec65968b0dbdcc8c8139dd5b66c2a79bdc8
MD5 39ebf7b4f2f94533d2c08788285e2409
BLAKE2b-256 1b2a36f08b24974eb4cad445443f10a497d7636069a252d38522e51c15541691

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