腾讯云调用api重装系统
1.准备SDK环境
1)安装腾讯云的python SDK
ps:如果同时具备 python2 及 python3 环境,则需使用 pip3 命令进行安装。
1 | pip install --upgrade tencentcloud-sdk-python |
2)如果安装SDK报以下错误,则需要升级pip的版本(如果可通过pip直接更新,则不需要做以下操作)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | Downloading /unpacking cos-python-sdk-v5 Downloading cos-python-sdk-v5-1.9.15. tar .gz (64kB): 64kB downloaded Running setup.py (path: /tmp/pip-build-C9EGzH/cos-python-sdk-v5/setup .py) egg_info for package cos-python-sdk-v5 Downloading /unpacking requests>=2.8 (from cos-python-sdk-v5) Downloading requests-2.27.1-py2.py3-none-any.whl (63kB): 63kB downloaded Cleaning up... Exception: Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/pip/basecommand.py" , line 122, in main status = self.run(options, args) File "/usr/lib/python2.7/dist-packages/pip/commands/install.py" , line 290, in run requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle) File "/usr/lib/python2.7/dist-packages/pip/req.py" , line 1266, in prepare_files req_to_install.extras): File "/usr/lib/python2.7/dist-packages/pkg_resources.py" , line 2401, in requires dm = self._dep_map File "/usr/lib/python2.7/dist-packages/pkg_resources.py" , line 2597, in _dep_map self.__dep_map = self._compute_dependencies() File "/usr/lib/python2.7/dist-packages/pkg_resources.py" , line 2621, in _compute_dependencies parsed = next(parse_requirements(distvers)) File "/usr/lib/python2.7/dist-packages/pkg_resources.py" , line 49, in <lambda> next = lambda o: o.next() File "/usr/lib/python2.7/dist-packages/pkg_resources.py" , line 2721, in parse_requirements "version spec" ) File "/usr/lib/python2.7/dist-packages/pkg_resources.py" , line 2686, in scan_list raise ValueError(msg, line, "at" , line[p:]) ValueError: ( 'Expected version spec in' , 'charset-normalizer ~=2.0.0' , 'at' , ' ~=2.0.0' ) Storing debug log for failure in /root/ .pip /pip .log |
解决方法:
1 2 3 4 5 | 1.wget --no-check-certificate https: //bootstrap .pypa.io /ez_setup .py 2.python ez_setup.py --insecure 3.wget https: //pypi .python.org /packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9 .0.1. tar .gz #md5=35f01da33009719497f01a4ba69d63c9 4. tar -xf pip-9.0.1. tar .gz && cd pip-9.0.1 5.python setup.py install |
2.根据api文档编写脚本
需要填写的字段:
1.SecretId与SecretKey需要填写你云账号密钥对应的ID与Key,请确保你拥有重装该实例的权限
# cred = credential.Credential("SecretId", "SecretKey")
2."ap-shanghai"这里改为你的服务器实例所在的地域,如果不知道可以去腾讯云文档查看
# client = cvm_client.CvmClient(cred, "ap-shanghai", clientProfile)
3.这里填写服务器的实例ID,与重装需要使用的镜像ID,镜像ID字段不是必填,不填则默认使用服务器本身的镜像进行重装
# "InstanceId": "实例ID",
# "ImageId": "镜像ID",
4.此处为实例的增强服务字段,非必填,SecurityService=开启云安全服务,MonitorService=开启云监控服务,默认开启,如果关闭则填false
# "EnhancedService": {
# "SecurityService": {
# "Enabled": True
# },
# "MonitorService": {
# "Enabled": True
脚本内容:
import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cvm.v20170312 import cvm_client, models
try:
cred = credential.Credential("SecretId", "SecretKey")
httpProfile = HttpProfile()
httpProfile.endpoint = "cvm.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = cvm_client.CvmClient(cred, "ap-shanghai", clientProfile)
req = models.ResetInstanceRequest()
params = {
"InstanceId": "实例ID",
"ImageId": "镜像ID",
"EnhancedService": {
"SecurityService": {
"Enabled": True
},
"MonitorService": {
"Enabled": True
}
}
}
req.from_json_string(json.dumps(params))
resp = client.ResetInstance(req)
print(resp.to_json_string())
except TencentCloudSDKException as err:
print(err)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!