利用Python调用云Api批量申请免费证书并下载至本地

2022-05-27 15:38:24 浏览数 (2)

# 本文已被官方文档收录https://cloud.tencent.com/document/product/400/71710

通常我们做一个站需要申请许多的ssl证书,例如主域名,cos源站域名,cos加速域名等等 自己一个一个去申请通常有点麻烦 那么如果你的域名的dns解析在dnspod的话,就可以使用以下代码进行批量申请

0.准备工作

使用本代码请先进行子用户创建并授权云API与SSL证书全部权限

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

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

1.SDK下载

请确保Python版本为3.6

查看Python版本

代码语言:javascript复制
python3 -V

升级一下pip,安装腾讯云Python SDK

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

2.代码部分

代码语言:python代码运行次数:0复制
import json,base64
from time import time,sleep
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.ssl.v20191205 import ssl_client, models

start = time()
cred = credential.Credential("SecretId", "SecretKey")
httpProfile = HttpProfile()
httpProfile.endpoint = "ssl.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
domain_name = []
while True:
    domain = input('要申请证书的域名:')
    if domain == '':
        break
    else:
        domain_name.append(domain)

for i in range(len(domain_name)):
    client = ssl_client.SslClient(cred, "", clientProfile)
    try:

        req = models.ApplyCertificateRequest()
        params = {
            "DvAuthMethod": "DNS_AUTO",
            "DomainName": domain_name[i]
        }
        req.from_json_string(json.dumps(params))

        resp = client.ApplyCertificate(req)
        response = json.loads(resp.to_json_string())
        print('域名:{0}资料已提交,五秒钟后自动验证'.format(domain_name[i]))
        certid = response['CertificateId']
        sleep(5)
        try:
            req1 = models.CompleteCertificateRequest()
            params1 = {
                "CertificateId": certid
            }
            req1.from_json_string(json.dumps(params1))

            resp1 = client.CompleteCertificate(req1)
            response1 = json.loads(resp1.to_json_string())
            print('域名:{0}验证成功!准备下载证书'.format(domain_name[i]))
            try:
                req2 = models.DownloadCertificateRequest()
                params2 = {
                    "CertificateId": certid
                }
                req2.from_json_string(json.dumps(params2))

                resp2 = client.DownloadCertificate(req2)
                response2 = json.loads(resp2.to_json_string())
                # print(response2['Content'])
                content = response2['Content']
                with open("{0}.zip".format(domain_name[i]), "wb") as f:

                    f.write(base64.b64decode(content))
                    f.close()
            except TencentCloudSDKException as err:
                print(err)
        except TencentCloudSDKException as err:
            print(err)
    except TencentCloudSDKException as err:
        print(err)
end = time()
print('本次代码执行共耗时:', round(end - start, 2), 's')

有问题欢迎在评论区留言,你的点赞收藏与关注将是作者创作的最大动力❤

本文章首次发布时间为2022.1.29 18:20

本文章为云 社区首发

未经同意禁止转载

本文将同步发布于csdn 知乎 哔哩哔哩专栏

0 人点赞