Aller au contenu
LICENTIVO / INTEGRATION

Ajouter une licence à une application Python avec PyInstaller

Créez un exécutable Windows qui vérifie la licence avant d’exporter un rapport. Testez la première activation, le redémarrage et le refus d’export avec l’application distribuée.

Windows x64 · Python 3.12 · PyInstaller 6.22.3Mis à jour

Protéger l’export en Python

Enregistrez l’exemple sous report.py. Renseignez l’identifiant du produit et ses clés publiques de confiance depuis la console. Préparez deux licences de test du même produit. Seule la première accorde le droit export.

Python · report.py
import os
import sys
from pathlib import Path

from licentivo import Client


PRODUCT_ID = "YOUR_PRODUCT_ID"
TRUSTED_KEYS = {"YOUR_KEY_ID": "YOUR_PUBLIC_KEY"}


def main(args):
    if len(args) != 1:
        print("Usage: LicenseReport <output.csv>", file=sys.stderr)
        return 64
    output = Path(args[0])
    try:
        with Client.open_product(
            PRODUCT_ID, TRUSTED_KEYS, os.getenv("LICENTIVO_LICENSE_KEY", "")
        ) as client:
            client.run_feature(
                "export", lambda: output.write_text("item,total\nreports,1\n", encoding="utf-8")
            )
    except Exception as error:
        print(error, file=sys.stderr)
        return 2
    print(output)
    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))

Créer l’exécutable Windows

Exécutez ces commandes PowerShell sur une machine Windows x64 équipée de Python 3.12. Le dossier dist contient l’unique exécutable à remettre au testeur.

PowerShell
py -3.12 -m venv .venv
.\.venv\Scripts\python.exe -m pip install "licentivo-sdk==0.11.8" "PyInstaller==6.22.3"
.\.venv\Scripts\python.exe -m PyInstaller --clean --noconfirm --onefile --name LicenseReport report.py

Activer l’application distribuée

Copiez uniquement LicenseReport.exe dans un dossier vide sur une machine de test Windows x64 sans Python. Exécutez les commandes dans ce dossier et saisissez la première clé dans l’invite masquée. L’export crée first.csv.

PowerShell
$key = Read-Host 'Test license key' -AsSecureString
try {
    $env:LICENTIVO_LICENSE_KEY = [System.Net.NetworkCredential]::new('', $key).Password
    .\LicenseReport.exe first.csv
    $LASTEXITCODE
} finally {
    Remove-Item Env:LICENTIVO_LICENSE_KEY -ErrorAction SilentlyContinue
    $key.Dispose()
}
Test-Path -LiteralPath .\first.csv

Redémarrer sous le même compte Windows

Relancez l’exécutable après avoir supprimé la variable contenant la clé. Un nouveau rapport confirme que le SDK a restauré les identifiants enregistrés pour ce compte Windows.

PowerShell
.\LicenseReport.exe restart.csv
$LASTEXITCODE
Get-Content -LiteralPath .\restart.csv

Tester une licence sans droit d’export

Passez à un nouveau profil Windows. Répétez l’activation avec la seconde clé et le nom de sortie denied.csv. Attendez un code de sortie 2 et aucun fichier. Choisissez un nom inédit pour chaque essai.

La compilation locale sous Windows x64 a utilisé CPython 3.12.10. Seul l’exécutable a été copié dans un dossier vide. Il a chargé Python depuis son propre dossier d’extraction et produit un rapport à l’activation et au redémarrage. Sans droit d’export, il a renvoyé 2 sans créer de fichier.

PowerShell
# In the fresh profile, supply the second key.
$key = Read-Host 'Test license key' -AsSecureString
try {
    $env:LICENTIVO_LICENSE_KEY = [System.Net.NetworkCredential]::new('', $key).Password
    .\LicenseReport.exe denied.csv
    $LASTEXITCODE
} finally {
    Remove-Item Env:LICENTIVO_LICENSE_KEY -ErrorAction SilentlyContinue
    $key.Dispose()
}
Test-Path -LiteralPath .\denied.csv