跳转到主要内容

Synology DSM通信的Python API

项目描述

https://github.com/hacf-fr/synologydsm-api/workflows/Tests/badge.svg Library version Supported versions Downloads Formated with Black

安装

[sudo] pip install synologydsm-api

使用

您可以将模块导入为 synology_dsm

构造函数

SynologyDSM(
    dsm_ip,
    dsm_port,
    username,
    password,
    use_https=False,
    verify_ssl=False,
    timeout=None,
    device_token=None,
    debugmode=False,
)

device_token 应在使用两步验证账户时添加,否则DSM将要求使用一次性密码 (OTP) 登录,请求将失败(有关更多信息,请参阅登录部分)。

默认 timeout 为10秒。

登录

库在首次请求时自动登录,但最好单独使用 login() 函数进行认证。

它将返回一个布尔值,表示是否成功或失败地认证到DSM。

如果您的账户需要两步验证 (2SA),login() 将引发 SynologyDSMLogin2SARequiredException。用一次性密码 (OTP) 作为参数再次调用该函数,如 login("123456")(最好是一个字符串以处理第一个零)。存储 device_token 属性,以便您在下一次打开新的 SynologyDSM 会话时无需再次使用密码。

代码示例

每个API都有一个update()函数,用于获取第一份数据,然后数据在下次update()调用时进行缓存和更新。

SynologyDSM类也可以一次性更新所有API。

from synology_dsm import SynologyDSM

print("Creating Valid API")
api = SynologyDSM("<IP/DNS>", "<port>", "<username>", "<password>")

print("=== Information ===")
api.information.update()
print("Model:           " + str(api.information.model))
print("RAM:             " + str(api.information.ram) + " MB")
print("Serial number:   " + str(api.information.serial))
print("Temperature:     " + str(api.information.temperature) + " °C")
print("Temp. warning:   " + str(api.information.temperature_warn))
print("Uptime:          " + str(api.information.uptime))
print("Full DSM version:" + str(api.information.version_string))
print("--")

print("=== Utilisation ===")
api.utilisation.update()
print("CPU Load:        " + str(api.utilisation.cpu_total_load) + " %")
print("Memory Use:      " + str(api.utilisation.memory_real_usage) + " %")
print("Net Up:          " + str(api.utilisation.network_up()))
print("Net Down:        " + str(api.utilisation.network_down()))
print("--")

print("=== Storage ===")
api.storage.update()
for volume_id in api.storage.volumes_ids:
    print("ID:          " + str(volume_id))
    print("Status:      " + str(api.storage.volume_status(volume_id)))
    print("% Used:      " + str(api.storage.volume_percentage_used(volume_id)) + " %")
    print("--")

for disk_id in api.storage.disks_ids:
    print("ID:          " + str(disk_id))
    print("Name:        " + str(api.storage.disk_name(disk_id)))
    print("S-Status:    " + str(api.storage.disk_smart_status(disk_id)))
    print("Status:      " + str(api.storage.disk_status(disk_id)))
    print("Temp:        " + str(api.storage.disk_temp(disk_id)))
    print("--")

print("=== Shared Folders ===")
api.share.update()
for share_uuid in api.share.shares_uuids:
    print("Share name:        " + str(api.share.share_name(share_uuid)))
    print("Share path:        " + str(api.share.share_path(share_uuid)))
    print("Space used:        " + str(api.share.share_size(share_uuid, human_readable=True)))
    print("Recycle Bin Enabled: " + str(api.share.share_recycle_bin(share_uuid)))
    print("--")

下载站使用

from synology_dsm import SynologyDSM

api = SynologyDSM("<IP/DNS>", "<port>", "<username>", "<password>")

if "SYNO.DownloadStation.Info" in api.apis:

    api.download_station.get_info()
    api.download_station.get_config()

    # The download list will be updated after each of the following functions:
    # You should have the right on the (default) directory that the download will be saved, or you will get a 403 or 406 error
    api.download_station.create("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")
    api.download_station.pause("dbid_1")
    # Like the other function, you can eather pass a str or a list
    api.download_station.resume(["dbid_1", "dbid_2"])
    api.download_station.delete("dbid_3")

    # Manual update
    api.download_station.update()

监控站使用

from synology_dsm import SynologyDSM

api = SynologyDSM("<IP/DNS>", "<port>", "<username>", "<password>")
surveillance = api.surveillance_station
surveillance.update() # First update is required

# Returns a list of cached cameras available
cameras = surveillance.get_all_cameras()

# Assuming there's at least one camera, get the first camera_id
camera_id = cameras[0].camera_id

# Returns cached camera object by camera_id
camera = surveillance.get_camera(camera_id)

# Returns cached motion detection enabled
motion_setting = camera.is_motion_detection_enabled

# Return bytes of camera image
surveillance.get_camera_image(camera_id)

# Updates all cameras/motion settings and cahce them
surveillance.update()

# Gets Home Mode status
home_mode_status =  surveillance.get_home_mode_status()

# Sets home mode - true is on, false is off
surveillance.set_home_mode(True)

系统使用

from synology_dsm import SynologyDSM

api = SynologyDSM("<IP/DNS>", "<port>", "<username>", "<password>")
system = api.system

# Reboot NAS
system.reboot()

# Shutdown NAS
system.shutdown()

# Manual update system information
system.update()

# Get CPU information
system.cpu_clock_speed
system.cpu_cores
system.cpu_family
system.cpu_series

# Get NTP settings
system.enabled_ntp
system.ntp_server

# Get system information
system.firmware_ver
system.model
system.ram_size
system.serial
system.sys_temp
system.time
system.time_zone
system.time_zone_desc
system.up_time

# Get list of all connected USB devices
system.usb_dev

升级使用

from synology_dsm import SynologyDSM

api = SynologyDSM("<IP/DNS>", "<port>", "<username>", "<password>")
upgrade = api.upgrade

# Manual update upgrade information
upgrade.update()

# check if DSM update is available
if upgrade.update_available:
    do something ...

# get available version string (return None if no update available)
upgrade.available_version

# get need of reboot (return None if no update available)
upgrade.reboot_needed

# get need of service restarts (return None if no update available)
upgrade.service_restarts

致谢/特别感谢

在此存储库中找到了Synology API“文档”:https://github.com/kwent/syno/tree/master/definitions

官方参考

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

synologydsm-api-1.0.2.tar.gz (23.2 kB 查看哈希值)

上传时间:

构建分布

synologydsm_api-1.0.2-py3-none-any.whl (26.2 kB 查看哈希值)

上传时间: Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面