A basic library for implementing a TCP client/server
Project description
TCPLib
NOTE: This module was made for educational purposes and should not be considered secure.
TCPLib is a module for setting up a simple TCP client and server. All data is sent and received as a bytes-like object (bytes or bytearray).
All data received is returned as a bytearray.
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
client = TCPClient()
client.connect(("127.0.0.1", 5000))
print(f"Connected to {client.host_addr[0]}@{client.host_addr[1]}")
client.send(b"Hello World!")
echo = client.receive()
print(f"Received message from server: {echo.decode('utf-8')}")
client.disconnect()
Output server.py
Server started
Message received: Hello World!
Server stopped
Process finished with exit code 0
Output client.py
Connected to 127.0.0.1@5000
Received message from server: Hello World!
Process finished with exit code 0
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(f"Listening for a connection...")
client.host_single_client(("127.0.0.1", 5000))
client_msg = client.receive()
print(f"Message received from {client.remote_addr[0]}@{client.remote_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@57003: Hello World!
Process finished with exit code 0
Installation
Requires Python 3.10 or higher
Install with pip:
pip install TCPLib
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tcplib-1.1.0.tar.gz.
File metadata
- Download URL: tcplib-1.1.0.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
552d5a529b4cd8a6a2f5174eee0fcd26162c01caaa3be4a2c693e255d556150d
|
|
| MD5 |
ba7c554a657e66c43b18cd2b1b8ae015
|
|
| BLAKE2b-256 |
69fb03dd59533ec29a299824ce88f5c4337e499bf3b19534b76965a68b8b3b34
|
File details
Details for the file tcplib-1.1.0-py3-none-any.whl.
File metadata
- Download URL: tcplib-1.1.0-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf017705b0d5cb8efc22f4181956f938ccf82e7743e5847f91646eee6f73dcf1
|
|
| MD5 |
9925ef8f0d7843d814fbda6dcb994a7d
|
|
| BLAKE2b-256 |
f599ac5d9711868fc498dd3a9a891e3ce595452823e8a968a173a85efeeb924b
|