Skip to main content

A basic library for implementing a TCP client/server

Project description

TCPLib


⚠️ This module was created for educational purposes and should not be considered secure.

TCPLib is a Python module for setting up simple TCP clients and servers. All data is sent as a bytes-like object (bytes or bytearray).
Data received by a TCPClient object is returned as a bytearray. Data received by a TCPServer object is returned as a Message object.


Example

server.py

from TCPLib import TCPServer

server = TCPServer()
server.start(("127.0.0.1", 5000))
print("Server started")

client_msg = server.pop_msg(block=True)
print(f"Message received: {client_msg.data.decode('utf-8')}")
server.send(client_msg.client_id, client_msg.data)

server.stop()
print("Server stopped")

client.py

from TCPLib import TCPClient

with TCPClient() as client:
    client.connect(("127.0.0.1", 5000))
    print(f"Connected to {client.peer_addr[0]}@{client.peer_addr[1]}")

    client.send(b"Hello World!")
    echo = client.receive()
    print(f"Received message from server: {echo.decode('utf-8')}")

Output (server.py)

Server started
Message received: Hello World!
Server stopped

Output (client.py)

Connected to 127.0.0.1@5000
Received message from server: Hello World!

It is also possible for a TCPClient object to host a single TCP/IP connection.
Below is an example where client.py connects to a host client instead of a server:

host_client.py

from TCPLib import TCPClient

client = TCPClient()
print("Listening for a connection...")
client.host_single_client(("127.0.0.1", 5000))

client_msg = client.receive()
print(f"Message received from {client.peer_addr[0]}@{client.peer_addr[1]}: {client_msg.decode('utf-8')}")
client.send(client_msg)

client.disconnect()

Output

Listening for a connection...
Message received from 127.0.0.1@50308: Hello World!

Installation

Requires Python 3.10 or higher

Install via pip:

pip install TCPLib


What's New in 2.0.0

General

  • Added improved and more consistent logging throughout the module.
  • Enhanced exception handling with clearer, more descriptive error messages.
  • Removed empty setters from all classes. Attempting to set a read-only property now raises AttributeError.
  • Address values are now validated, and invalid addresses will raise ValueError.

TCPServer

  • Updated client ID generation to a simpler, more reliable method with better uniqueness.
  • The server now tracks timeouts for each client. You can configure both timeout and max_timeouts per client using set_client_attributes() (formerly set_clients_timeout()).
  • The get_all_msg() method no longer accepts block or timeout parameters. Its purpose is to retrieve already-queued messages, so blocking is unnecessary in hindsight.

TCPClient

  • Now supports usage with a context manager, automatically calling disconnect() on exit.
  • Renamed host_addr() and remote_addr() to local_addr() and peer_addr() for clarity. The former naming caused confusion as the role of the client changed dynamically, requiring the user to know the classes role or call is_host() to find out. Now:
    • local_addr() always returns the address assigned to the class.
    • peer_addr() always returns the address of the connected remote peer.
  • Renamed send_bytes() and receive_bytes() to send_raw() and receive_raw(), respectively. The old names were misleading as all data in this library is sent as bytes. The new names for these methods better reflect their intended purpose.

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

tcplib-2.0.0.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

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

tcplib-2.0.0-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file tcplib-2.0.0.tar.gz.

File metadata

  • Download URL: tcplib-2.0.0.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for tcplib-2.0.0.tar.gz
Algorithm Hash digest
SHA256 fda1bb45c202a05d96d0724259df1b5ceb628ec6a60476c58c5034ae707ff381
MD5 c4838171a971769e466fe968e68c27f3
BLAKE2b-256 bd5e4b45e2e65d51ff39b22440f55435a2d17d4173f795d5586f47c0fe28adfd

See more details on using hashes here.

File details

Details for the file tcplib-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: tcplib-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for tcplib-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48503a5c4dac2a3361ca3e65601ac7e7d10bdfcd6451f2814c9c25c0dd417244
MD5 97254bfc956a563a650c59c3dec7b61e
BLAKE2b-256 09932db56d98c18cf50f65ca1a58271716a7f740e372eded71ef97bd5b008bfb

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