Vai al contenuto
LICENTIVO / INTEGRATION

Aggiungere una licenza a un’app Python distribuita con PyInstaller

Crea un eseguibile Windows che verifichi la licenza prima di esportare un report. Prova prima attivazione, riavvio ed esportazione negata nell’app distribuita.

Windows x64 · Python 3.12 · PyInstaller 6.22.3Aggiornato

Proteggere l’esportazione in Python

Salva l’esempio come report.py e inserisci l’ID prodotto e le chiavi pubbliche attendibili della console. Prepara due licenze di prova per lo stesso prodotto. Assegna il permesso export solo alla prima.

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:]))

Creare l’eseguibile Windows

Esegui questi comandi PowerShell su una macchina Windows x64 con Python 3.12. La cartella dist contiene il singolo eseguibile da consegnare per la prova.

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

Attivare l’app distribuita

Copia solo LicenseReport.exe in una cartella vuota su una macchina di prova Windows x64 senza Python. Esegui i comandi dalla cartella e inserisci la prima chiave quando viene richiesta con input nascosto. Se l’operazione riesce, viene creato 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

Riavviare con lo stesso account Windows

Avvia di nuovo l’eseguibile dopo aver eliminato la variabile della chiave. Un nuovo report conferma che l’SDK ha recuperato le credenziali memorizzate per questo account Windows.

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

Verificare una licenza senza permesso di esportazione

Passa a un nuovo profilo utente Windows. Ripeti l’attivazione con la seconda chiave e il nome di destinazione denied.csv. Sono attesi il codice di uscita 2 e nessun file. Usa un nome diverso a ogni prova.

La build locale per Windows x64 ha usato CPython 3.12.10. In una cartella vuota è stata copiata soltanto la EXE, che ha caricato Python dalla propria cartella di estrazione. Ha scritto i report all’attivazione e al riavvio. Senza permesso ha restituito 2 senza creare file.

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