前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用Python调用云Api实现全地域同步轻量应用服务器密钥并共享给云服务器

利用Python调用云Api实现全地域同步轻量应用服务器密钥并共享给云服务器

原创
作者头像
小宇-xiaoyu
修改2022-04-06 12:32:13
1.6K0
修改2022-04-06 12:32:13
举报
文章被收录于专栏:玩转Lighthouse.玩转Lighthouse.

0.准备工作

使用本代码请先进行子用户创建并授权云API与轻量应用服务器全部权限

请注意 为了保障您的账户以及云上资产的安全 请谨慎保管SecretId 与 SecretKey 并定期更新 删除无用权限

前往创建子用户:https://console.cloud.tencent.com/cam

1.SDK下载

请确保Python版本为3.6+

查看Python版本

代码语言:javascript
复制
python3 -V

安装腾讯云Python SDK

代码语言:javascript
复制
pip install -i https://mirrors.tencent.com/pypi/simple/ --upgrade tencentcloud-sdk-python

2.代码部分

代码语言:javascript
复制
import json
import tencentcloud.cvm.v20170312.cvm_client
import tencentcloud.cvm.v20170312.models
from time import time
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.lighthouse.v20200324 import lighthouse_client, models


start = time()
lh_aria = ['ap-chengdu', 'ap-guangzhou', 'ap-hongkong', 'ap-shanghai', 'ap-singapore', 'na-siliconvalley', 'eu-moscow',
           'ap-tokyo', 'ap-nanjing', 'ap-mumbai', 'eu-frankfurt']

# 此处添加SecretId 与 SecretKey
cred = credential.Credential("SecretId", "SecretKey")

httpProfile = HttpProfile()
httpProfile.endpoint = "lighthouse.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
key_name = input('key_name=')

client = lighthouse_client.LighthouseClient(cred, 'ap-beijing', clientProfile)

try:
    req = models.CreateKeyPairRequest()
    params = {
        "KeyName": key_name
    }
    req.from_json_string(json.dumps(params))

    resp = client.CreateKeyPair(req)

    response = json.loads(resp.to_json_string())
    # print(response)
    public_key = response['KeyPair']['PublicKey']
    private_key = response['KeyPair']['PrivateKey']
    # key_aria = key_name+'_'+aria[i]

    key = open(key_name, mode='w')
    key.write(private_key)
    key.close()
    print('私钥已输出至文件:', key_name)
    # print(resp.to_json_string())
    for i in range(11):
        try:
            client1 = lighthouse_client.LighthouseClient(cred, lh_aria[i], clientProfile)
            req1 = models.ImportKeyPairRequest()
            params1 = {
                "KeyName": key_name,
                "PublicKey": public_key
            }
            req1.from_json_string(json.dumps(params1))

            resp1 = client1.ImportKeyPair(req1)
            print(resp1.to_json_string())

        except TencentCloudSDKException as err:
            print(err)

    try:

        httpProfile2 = HttpProfile()
        httpProfile2.endpoint = "cvm.tencentcloudapi.com"

        clientProfile2 = ClientProfile()
        clientProfile2.httpProfile2 = httpProfile2
        client2 = tencentcloud.cvm.v20170312.cvm_client.CvmClient(cred, 'ap-beijing', clientProfile2)

        req2 = tencentcloud.cvm.v20170312.models.ImportKeyPairRequest()
        params2 = {
            "KeyName": key_name,
            "ProjectId": 0,
            "PublicKey": public_key
        }
        req2.from_json_string(json.dumps(params2))

        resp2 = client2.ImportKeyPair(req2)
        print(resp2.to_json_string())
    except TencentCloudSDKException as err:
        print(err)

except TencentCloudSDKException as err:
    print(err)
end = time()
print('本次代码执行共耗时:', round(end - start, 2), 's')

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0.准备工作
  • 1.SDK下载
  • 2.代码部分
相关产品与服务
轻量应用服务器
轻量应用服务器(TencentCloud Lighthouse)是新一代开箱即用、面向轻量应用场景的云服务器产品,助力中小企业和开发者便捷高效的在云端构建网站、Web应用、小程序/小游戏、游戏服、电商应用、云盘/图床和开发测试环境,相比普通云服务器更加简单易用且更贴近应用,以套餐形式整体售卖云资源并提供高带宽流量包,将热门开源软件打包实现一键构建应用,提供极简上云体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com