A Python client for TP-Link router SMS functionality
Project description
TP-Link SMS Client
A Python package for interacting with SMS and USSD functionality on TP-Link routers, such as the TL-MR6400. This package allows you to send SMS messages, read messages (unread, received, sent, draft), retrieve SMS configuration, and send USSD codes via the router's API.
This project is a Python port of the node-tplink-api Node.js library, adapted for Python with asynchronous HTTP requests and special character encoding for compatibility with TP-Link routers.
Features
- Authenticate with the TP-Link router's admin interface.
- Send SMS messages with proper encoding for newline (
\n) and carriage return (\r) characters. - Read SMS messages from various boxes (unread, received, sent, draft) with pagination support.
- Retrieve SMS configuration settings (e.g., enable status, service center number).
- Send and receive USSD codes (e.g.,
*123#for balance checks).
Installation
Prerequisites
- Python 3.7 or higher
- A TP-Link router with SMS/USSD support (e.g., TL-MR6400)
- Network access to the router (e.g.,
http://192.168.1.1)
Install via pip
-
Clone the repository or download the package:
git clone https://github.com/mosiiisom/tplink_sms.git cd tplink_sms
-
Install the package locally:
pip install .
For development, use editable mode:
pip install -e .
Dependencies
Listed in requirements.txt:
requests>=2.28.0
If you use aiohttp for asynchronous HTTP requests, add:
aiohttp>=3.8.0
Install dependencies manually if needed:
pip install -r requirements.txt
Usage
The TPLinkSMSClient class provides a high-level interface for interacting with the router's SMS and USSD features. Below are example usage scenarios.
Example: Basic Operations
import asyncio
from tplink_sms.client import TPLinkSMSClient
async def main():
# Initialize the client
client = TPLinkSMSClient(
base_url="http://192.168.1.1",
password="your_router_password",
username="admin"
)
# Get SMS configuration
config = await client.get_sms_config()
print("SMS Config:", config)
# Get total number of unread messages
total_unread = await client.get_total_messages("unread")
print(f"Total Unread Messages: {total_unread}")
# Read unread messages (first page)
messages = await client.read_messages("unread", page_number=1)
print("Unread Messages:", messages)
# Send an SMS
result = await client.send_sms("9123456789", "Hello,\nTest message.")
print("Send SMS Result:", result)
# Send a USSD code
ussd_result = await client.send_ussd_code("*123#")
print("USSD Result:", ussd_result)
if __name__ == "__main__":
asyncio.run(main())
Example: Reading All Messages with Pagination
import asyncio
from tplink_sms.client import TPLinkSMSClient
async def main():
client = TPLinkSMSClient(
base_url="http://192.168.1.1",
password="your_router_password"
)
# Read all received messages
box_types = ["unread", "received", "sent", "draft"]
page_size = 10 # Adjust based on router's page size
for box_type in box_types:
total = await client.get_total_messages(box_type)
print(f"Total {box_type} messages: {total}")
messages = []
for page in range(1, (total // page_size) + 2):
result = await client.read_messages(box_type, page_number=page)
if result and result.get("error") == 0:
for action in result.get("actions", []):
if action["req"][1].endswith("MSGENTRY"):
messages.extend(action["res"].get("attributes", []))
print(f"{box_type.capitalize()} messages page {page}:", result)
print(f"All {box_type} messages:", messages)
if __name__ == "__main__":
asyncio.run(main())
Configuration
- Base URL: The router's IP address (e.g.,
http://192.168.1.1). Ensure your machine is on the same network. - Password: The admin password for the router's web interface.
- Username: Typically
"admin"(default for TP-Link routers). - Character Encoding: The package automatically encodes newlines (
\n) aschr(18)and carriage returns (\r) aschr(17) for SMS and USSD content, as required by the TL-MR6400.
Notes
- Authentication: The client re-authenticates before each operation to ensure session validity. This is necessary due to the router's session timeout behavior.
- Pagination: The router may limit messages per page (default assumed to be 10). Adjust
page_sizeif needed. - Error Handling: Check the
errorfield in API responses (0indicates success). Debug issues by enabling logging infetch_cgi_gdpr.py.
Contributing
Contributions are welcome! To contribute:
- Fork the repository:
https://github.com/mosiiisom/tplink_sms - Create a feature branch:
git checkout -b feature/your-feature - Commit changes:
git commit -m "Add your feature" - Push to the branch:
git push origin feature/your-feature - Open a pull request.
Please include tests and update documentation as needed.
Issues
Report bugs or feature requests at: https://github.com/mosiiisom/tplink_sms/issues
For sensitive issues, contact the maintainer via a private email (not the noreply address) or through GitHub Issues.
License
MIT License
Project details
Release history Release notifications | RSS feed
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 tpsms-0.1.2.tar.gz.
File metadata
- Download URL: tpsms-0.1.2.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49bd6dc7350f4c970cceed39e987972c68c69fa06b79dab06b3fad3d2e9eb1c3
|
|
| MD5 |
d120fb76693c3c61d1f09f45881fd119
|
|
| BLAKE2b-256 |
e8e58672aeff55fa7967237db10e9c153865323cead2d39bd3a85dd5026f3420
|
Provenance
The following attestation bundles were made for tpsms-0.1.2.tar.gz:
Publisher:
python-publish.yml on mosiiisom/tpsms
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tpsms-0.1.2.tar.gz -
Subject digest:
49bd6dc7350f4c970cceed39e987972c68c69fa06b79dab06b3fad3d2e9eb1c3 - Sigstore transparency entry: 434309412
- Sigstore integration time:
-
Permalink:
mosiiisom/tpsms@565d2e6cf2b66dea2032ba4db696a27e93eeee61 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/mosiiisom
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@565d2e6cf2b66dea2032ba4db696a27e93eeee61 -
Trigger Event:
push
-
Statement type:
File details
Details for the file tpsms-0.1.2-py3-none-any.whl.
File metadata
- Download URL: tpsms-0.1.2-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8119bb2d165bf7a3aa31d0d29e77b1a3d42f62faf9c50059aa38c5b574ff511b
|
|
| MD5 |
b8c3f07f6bb12f21a39fe24cc93fdddb
|
|
| BLAKE2b-256 |
d47bb03c0edc79a0604561b8c6302c81a6f983ab404b352e2b5e3eb903d9feef
|
Provenance
The following attestation bundles were made for tpsms-0.1.2-py3-none-any.whl:
Publisher:
python-publish.yml on mosiiisom/tpsms
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tpsms-0.1.2-py3-none-any.whl -
Subject digest:
8119bb2d165bf7a3aa31d0d29e77b1a3d42f62faf9c50059aa38c5b574ff511b - Sigstore transparency entry: 434309445
- Sigstore integration time:
-
Permalink:
mosiiisom/tpsms@565d2e6cf2b66dea2032ba4db696a27e93eeee61 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/mosiiisom
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@565d2e6cf2b66dea2032ba4db696a27e93eeee61 -
Trigger Event:
push
-
Statement type: