中文 English

A Python-based way to keep China Telecom Cloud PC awake

Published: 2024-06-18
ctyun python chrome tianyi-cloud cloud-pc

Principle: Regularly connect to China Telecom Cloud Desktop (interval must be less than 1 hour)

Step 1: Obtain Request Data
  1. Open Chrome browser’s Developer Tools:

    • In Chrome browser, press F12 or right-click and select Inspect to open Developer Tools.
  2. Visit and log in to China Telecom Cloud Desktop:

    • Visit https://pc.ctyun.cn and log in to Cloud PC with your account.
  3. Copy the curl command for the connect request:

    • In Developer Tools, select the Network tab, find and click on the request named connect.
    • Right-click on this request and select Copy as cURL (cmd).
  4. Obtain authData from localStorage:

    • In Developer Tools, select the Application tab.
    • In the left sidebar, find Local Storage and select https://pc.ctyun.cn.
    • Find the authData key and copy its corresponding value.
Step 2: Fill into the Python Script

Copy the relevant data from the curl command obtained above and the authData value from localStorage to a temporary notepad, find the required fields in the Python script below, and

fill them into the following Python script:

import requests
import hashlib
import time

# 配置参数(用户需要根据实际情况修改)
device_info = {
    "objId": "你的设备ID",  # 设备ID
    "objType": 0,  # 设备类型
    "osType": 15,  # 操作系统类型
    "deviceId": 60,  # 设备ID
    "deviceCode": "你的设备代码",  # 设备代码
    "deviceName": "你的设备名称",  # 设备名称
    "sysVersion": "你的系统版本",  # 系统版本
    "appVersion": "1.36.1",  # 应用版本
    "hostName": "你的主机名称",  # 主机名称
    "vdCommand": "",  # 虚拟桌面命令
    "ipAddress": "",  # IP地址
    "macAddress": "",  # MAC地址
    "hardwareFeatureCode": "你的硬件特征码"  # 硬件特征码
}

# 请求头中的一些参数
app_model_value = "2"
device_code_value = "你的设备代码"
device_type_value = "60"
request_id_value = "你的请求ID值"
tenant_id_value = "你的租户ID值"
userid_value = "你的用户ID值"
version_value = "201360101"
secret_key_value = "你的秘钥值"

# 动态生成时间戳
timestamp_value = str(int(time.time() * 1000))

# 创建签名字符串
signature_str = device_type_value + request_id_value + tenant_id_value + timestamp_value + userid_value + version_value + secret_key_value

# 使用MD5算法创建签名
hash_obj = hashlib.md5()
hash_obj.update(signature_str.encode('utf-8'))
digest_hex = hash_obj.hexdigest().upper()

# 准备请求头
headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
    'content-type': 'application/x-www-form-urlencoded',
    'ctg-appmodel': app_model_value,
    'ctg-devicecode': device_code_value,
    'ctg-devicetype': device_type_value,
    'ctg-requestid': request_id_value,
    'ctg-signaturestr': digest_hex,
    'ctg-tenantid': tenant_id_value,
    'ctg-timestamp': timestamp_value,
    'ctg-userid': userid_value,
    'ctg-version': version_value,
    'origin': 'https://pc.ctyun.cn',
    'priority': 'u=1, i',
    'referer': 'https://pc.ctyun.cn/',
    'sec-ch-ua': '"Google Chrome";v="125", "Chromium";v="125", "Not.A/Brand";v="24"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36'
}

# 设置API的URL和路径
url = "https://desk.ctyun.cn:8810/api/"
computer_connect = "desktop/client/connect"

# 发起POST请求连接云电脑
response = requests.post(url + computer_connect, data=device_info, headers=headers)

# 打印出云电脑的连接状态
print("云电脑连接状态:", response.json())
Step 3: Schedule Script Execution

You can use the following methods to execute the script periodically:

  1. Windows Task Scheduler:

    • Open Task Scheduler and create a new task.
    • In the Actions tab, select Start a program, then choose your Python interpreter and script path.
  2. Linux Crontab:

    • Open terminal and input crontab -e.
    • Add a line to run the script periodically, for example, run once every hour: 0 * * * * /usr/bin/python3