Skip to main content

迅雷 XL Download Python SDK

Project description

迅雷下载 Python SDK 指南

XL Download Python SDK 适用于在 Python 脚本或桌面工具中接入迅雷下载能力。

支持平台

Windows x64 Windows x86 macOS Universal Linux x64

环境要求

  • Python 3.8 或更高版本。

包说明

  • xunlei-open-dlsdk:主 API 包。
  • xunlei-open-dlsdk-native-windows-x64
  • xunlei-open-dlsdk-native-windows-x86
  • xunlei-open-dlsdk-native-macos-universal
  • xunlei-open-dlsdk-native-linux-x64

pip 安装

在应用虚拟环境中安装主包,并按应用要支持的平台安装对应 native 包。跨平台应用可以同时安装多个 native 包,运行时会加载当前平台对应的动态库。

目标平台 安装命令
Windows x64 python -m pip install xunlei-open-dlsdk xunlei-open-dlsdk-native-windows-x64
Windows x86 python -m pip install xunlei-open-dlsdk xunlei-open-dlsdk-native-windows-x86
macOS Universal python -m pip install xunlei-open-dlsdk xunlei-open-dlsdk-native-macos-universal
Linux x64 python -m pip install xunlei-open-dlsdk xunlei-open-dlsdk-native-linux-x64

例如接入 macOS:

python -m pip install xunlei-open-dlsdk xunlei-open-dlsdk-native-macos-universal

Python import 名称保持为 xl_dl

from xl_dl import XLDownloadAPI

也可以不安装 native wheel,直接指定本地原生库路径:

export XL_DL_NATIVE_PATH=/absolute/path/to/libdk.dylib
平台 文件名
Windows x64 / x86 dk.dll
macOS Universal libdk.dylib
Linux x64 libdk.so

XL_DL_NATIVE_PATH 必须是绝对路径。适用于无法安装 native wheel 的场景(如 CI 环境、自定义存放路径)。

最小示例

import time
from pathlib import Path

from xl_dl import ERROR_ALREADY_INIT, ERROR_SUCCESS, TASK_STATUS_SUCCEEDED, TASK_STATUS_FAILED, XLDownloadAPI

app_id = "your-app-id"
api_key = "your-api-key"
config_path = "/tmp/xl_dl_sdk_conf"
save_path = "/tmp/ThunderDownload"
Path(config_path).mkdir(parents=True, exist_ok=True)
Path(save_path).mkdir(parents=True, exist_ok=True)

sdk = XLDownloadAPI()

init_result = sdk.init(app_id, "1.0", config_path, save_tasks=True)
if init_result not in (ERROR_SUCCESS, ERROR_ALREADY_INIT):
    raise RuntimeError(f"init failed: {init_result}")

try:
    code, login_token, _expires_in, message = sdk.get_login_token(api_key)
    if code != 0:
        raise RuntimeError(f"get loginToken failed: {message}")

    login_result, session_id = sdk.login(login_token)
    if login_result != ERROR_SUCCESS:
        raise RuntimeError(f"login failed: {login_result}")

    create_result, task_id = sdk.create_p2sp_task(
        "https://example.com/file.zip",
        save_path,
        "file.zip",
    )
    if create_result != ERROR_SUCCESS:
        raise RuntimeError(f"create task failed: {create_result}")

    start_result = sdk.start_task(task_id)
    if start_result != ERROR_SUCCESS:
        raise RuntimeError(f"start task failed: {start_result}")

    while True:
        result, state = sdk.get_task_state(task_id)
        if state is None:
            break
        print(f"\rstate:{state.state_code} downloaded:{state.downloaded_size}/{state.total_size} speed:{state.speed}", end="", flush=True)
        if state.state_code in (TASK_STATUS_SUCCEEDED, TASK_STATUS_FAILED):
            print()
            break
        time.sleep(1)

finally:
    sdk.uninit()

完整可运行示例见 示例代码

相关文档

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

xunlei_open_dlsdk-1.0.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xunlei_open_dlsdk-1.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file xunlei_open_dlsdk-1.0.1.tar.gz.

File metadata

  • Download URL: xunlei_open_dlsdk-1.0.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for xunlei_open_dlsdk-1.0.1.tar.gz
Algorithm Hash digest
SHA256 cfaa6daca4868d64f983855a9649c3ab0fe2863d6412ef16bc866f85ba9883ad
MD5 a2541a2a55dcfb6eeb58894f793cbb55
BLAKE2b-256 8585e375cdfa21d359a3b65e6db96dac888079aee652aab768d56af17ed2cb0a

See more details on using hashes here.

File details

Details for the file xunlei_open_dlsdk-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for xunlei_open_dlsdk-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 38e2f8123643b58be8cbe11cb81ec74743bedb3848304f14f109323b91f1f317
MD5 4e9179e20f6c5807017279e8c2b8e3b8
BLAKE2b-256 a5bd4b6b036890fb38df62fde5c5f831ebd41cc392bbf6badd3965605cc433c4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page