Skip to main content

Sagemcom API Client in Python

(Unofficial) async Python client to interact with Sagemcom F@st routers via internal API's. This client offers helper functions to retrieve common used functions, but also offers functionality to do custom requests via XPATH notation.

Python 3.11+ required.

Features

  • Retrieve detailed information of your Sagemcom F@st device
  • Retrieve connected devices (wifi and ethernet)
  • Reboot Sagemcom F@st device
  • Retrieve and set all values of your Sagemcom F@st device

Supported devices

The Sagemcom F@st series is used by multiple cable companies, where some cable companies did rebrand the router. Examples are the b-box from Proximus, Home Hub from bell and the Smart Hub from BT.

Router Model Provider(s) Authentication Method Comments
Sagemcom F@st 3864 Optus sha512 username: guest, password: ""
Sagemcom F@st 3865b Proximus (b-box3) md5
Sagemcom F@st 3890V3 Delta / Zeelandnet sha512
Sagemcom F@st 3890V3 DNA (DNA Mesh Wifi F-3890) sha512 username: admin
Sagemcom F@st 3896 Ziggo1 sha512 username: admin
Sagemcom F@st 4360Air KPN md5
Sagemcom F@st 4353 Belong Gateway md5 username: admin, password: ""
Sagemcom F@st 5250 Bell (Home Hub 2000) md5 username: guest, password: ""
Sagemcom F@st 5280 sha512
Sagemcom F@st 5290 / FWR226e Frontier md5 username: admin
Sagemcom F@st 5359 KPN (Box 12) sha512 username: admin
Sagemcom F@st 5364 BT (Smart Hub) md5 username: guest, password: ""
SagemCom F@st 5366SD Eir F3000 md5
Sagemcom F@st 5370e Telia sha512
Sagemcom F@st 5380 TDC md5
Sagemcom F@st 5566 Bell (Home Hub 3000) md5 username: guest, password: ""
Sagemcom F@st 5598 YouFibre None / New API username: admin, password: ""
Sagemcom F@st 5688T Salt (FibreBox_X6) sha512 username: admin
Sagemcom F@st 5689 Bell (Home Hub 4000) md5 username: admin, password: ""
Sagemcom F@st 5689E Bell (Giga Hub) sha512 username: admin, password: ""
Sagemcom F@st 5690 Bell (Giga Hub) sha512 username: admin, password: ""
Sagemcom F@st 5655V2 MásMóvil md5
Sagemcom F@st 5657IL md5
Sagemcom F@st 5360 Sunrise Internet Box2 md5 username: admin
Speedport Pro Telekom md5 username: admin

1 The firmware provided on the Sagemcom F@st 3896 router from Ziggo does not support the endpoint used in this library. sagemcom-f3896lg-zg-api provides an API client suitable for Ziggo's firmware.

2 If you run into SSLV3_ALERT_HANDSHAKE_FAILURE errors, see here for a workaround.

Contributions welcome. If you router model is supported by this package, but not in the list above, please create an issue or pull request.

Installation

pip install sagemcom_api

Getting Started

Depending on the router model, Sagemcom is using different encryption methods for authentication, which can be found in the table above. This package supports MD5 and SHA512 encryption. If you receive a LoginTimeoutException, you will probably need to use another encryption type.

The following script can be used as a quickstart.

import asyncio
from sagemcom_api.client import SagemcomClient
from sagemcom_api.enums import ApiMode, EncryptionMethod
from sagemcom_api.exceptions import NonWritableParameterException

HOST = ""
USERNAME = ""
PASSWORD = ""
ENCRYPTION_METHOD = EncryptionMethod.SHA512 # or EncryptionMethod.MD5
VALIDATE_SSL_CERT = True
API_MODE = ApiMode.AUTO  # auto, legacy or rest

async def main() -> None:
    async with SagemcomClient(
        HOST,
        USERNAME,
        PASSWORD,
        ENCRYPTION_METHOD,
        api_mode=API_MODE,
        verify_ssl=VALIDATE_SSL_CERT,
    ) as client:
        try:
            await client.login()
        except Exception as exception:  # pylint: disable=broad-except
            print(exception)
            return

        # Print device information of Sagemcom F@st router
        device_info = await client.get_device_info()
        print(f"{device_info.id} {device_info.model_name}")

        # Print connected devices
        devices = await client.get_hosts()

        for device in devices:
            if device.active:
                print(f"{device.id} - {device.name}")

        # Retrieve values via XPath notation, output is a dict
        custom_command_output = await client.get_value_by_xpath("Device/UserInterface/AdvancedMode")
        print(custom_command_output)

        # Set value via XPath notation and catch specific errors
        try:
            custom_command_output = await client.set_value_by_xpath("Device/UserInterface/AdvancedMode", "true")
        except NonWritableParameterException as exception:  # pylint: disable=broad-except
            print("Not allowed to set AdvancedMode parameter on your device.")
            return

        print(custom_command_output)

asyncio.run(main())

Functions

  • login()
  • get_device_info()
  • get_hosts()
  • get_port_mappings()
  • reboot()
  • get_value_by_xpath(xpath)
  • set_value_by_xpath(xpath, value)

Advanced

API Mode

The client supports two API variants:

  • ApiMode.LEGACY: original /cgi/json-req API with XPath support
  • ApiMode.REST: newer /api/v1/* API used by newer firmwares
  • ApiMode.AUTO (default): tries legacy first, then falls back to REST when the legacy endpoint is unavailable

When REST mode is active, high-level helpers like get_device_info(), get_hosts() and reboot() are supported. XPath-based methods (get_value_by_xpath, set_value_by_xpath, get_values_by_xpaths) are legacy-only.

REST firmware sets its session cookie on the router's IP address. Sessions created by the client accept such cookies in REST mode only; if you pass your own session for a REST router, create it with CookieJar(unsafe=True).

Determine the EncryptionMethod

If you are not sure which encryption method to use, you can leave it empty or pass None and use get_encryption_method to determine the encryption method.

get_encryption_method will return an EncryptionMethod when a match is found, EncryptionMethod.NONE when the router uses the REST API, and None if no method matches. Best would be to use this function only during your initial investigation.

This function will throw a LoginTimeoutException when no match is found, since this is still a HTTP Time Out. This could caused by the wrong encryption method, but also by trying to connect to an inaccessible host.

It will throw a LoginRetryErrorException when the router has locked logins after too many attempts. Wait for the lock to expire before trying again.

Handle exceptions

Some functions may cause an error when an attempt is made to execute it. These exceptions are thrown by the client and need to be handled in your Python program. Best practice is to catch some specific exceptions and handle them gracefully.

from sagemcom_api.exceptions import *

try:
    await client.set_value_by_xpath("Device/UserInterface/AdvancedMode", "true")
except NonWritableParameterException as exception:
    print("You don't have rights to write to this parameter.")
except UnknownPathException as exception:
    print("The xpath does not exist.")

Run your custom commands

Not all values can be retrieved by helper functions in this client implementation. By using XPath, you are able to return all values via the API. The result will be a dict response, or an exception when the attempt was not successful.

try:
    result = await client.get_value_by_xpath("Device/DeviceSummary")
except Exception as exception:
    print(exception)

Use your own aiohttp ClientSession

ClientSession is the heart and the main entry point for all client API operations. The session contains a cookie storage and connection pool, thus cookies and connections are shared between HTTP requests sent by the same session.

In order to change settings like the time-out, it is possible to pass your custom aiohttp ClientSession.

from aiohttp import ClientSession, ClientTimeout

session = ClientSession(timeout=ClientTimeout(100))
client = SagemcomClient(session=session)

Debugging

  • Unable to login (XMO_AUTHENTICATION_ERR)

See advanced instructions for debugging common issues.

Inspired by

Metadata

Release files for sagemcom_api 1.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sagemcom_api 1.6.0
File Size Uploaded Provenance Actions
sagemcom_api-1.6.0.tar.gz 17.3 kB Without provenance Details for sagemcom_api-1.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sagemcom_api 1.6.0
File Interpreter ABI Platform Actions
sagemcom_api-1.6.0-py3-none-any.whl Python 3 none any Details for sagemcom_api-1.6.0-py3-none-any.whl

Total release size: 35.6 kB

Release files / sagemcom_api-1.6.0.tar.gz

Download URL sagemcom_api-1.6.0.tar.gz
Size 17.3 kB
Tags Source
SHA-256 checksum
How to use checksums
3c82bf0dc4af4a694f8f7f4b2ec3d5eb2d64a386344b28666ba7f2bc370d6420
BLAKE2b-256 checksum
How to use checksums
7219b20cfe92a60177e9c170da18eecdbbad05a66ccf0facc4ab7def631a44a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / sagemcom_api-1.6.0-py3-none-any.whl

Download URL sagemcom_api-1.6.0-py3-none-any.whl
Size 18.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a186a9ea40360dae24f7d26183ab65a548b37a88817e7b8fed78b302776f2f11
BLAKE2b-256 checksum
How to use checksums
8709841616665a45659c7734e4948c91c18d7ab77e0a6a37dcd91b2b84da29de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for sagemcom_api 1.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page