SSL 证书 使用 Python 调用 API 批量申请免费证书并下载至本地

这篇说说如何使用腾讯云 API 批量申请并下载SSL证书,包括环境准备、Python SDK安装及代码实现步骤,核心操作通过调用SSL证书接口自动完成域名验证与证书下载。 环境与前提:需子账号授权、安装Python及PyCharm,并通过pip安装腾讯云Python SDK。 代码实现:使用Python脚本循环输入域名,自动调用ApplyCertificate、CompleteCertificate和Download。

相关产品
SSL 证书 产品页,含版本规格、价格与购买入口。

一、概述

这篇会指导你介绍如何使用腾讯云 API 批量申请证书并下载证书。

二、前提条件

·子用户创建并授权云 API 与 SSL 证书全部权限。

·已安装 Python 最新版本,如需安装,请前往 Python 页面 进行下载。

·已安装 PyCharm 最新版本,如需安装,请前往 PyCharm 页面 进行下载。

三、操作步骤

1. 打开命令提示符,查看 Python 版本。命令行如下:

python -V

2. 查看 Python 目前已经安装的第三方模块,命令行如下:

pip list

注意:例如缺少 requests,可通过 pip install requests 安装该模块。

3. 通过 pip 安装腾讯云 Python SDK。命令行如下:

pip install -i https://mirrors.tencent.com/pypi/simple/ –upgrade tencentcloud-sdk-python

4. 前往 Github 仓库 或者 Gitee 仓库 下载最新代码至本地,并进行解压。

5. 打开 PyCharm,导入最新的代码文件,进入 tencentcloud-sdk-python/tencentcloud/ssl 目录下并创建新的 Python 文件,例如 apply.py。添加以下代码并执行。

import json,base64,osfrom time import time,sleepfrom tencentcloud.common import credentialfrom tencentcloud.common.profile.client_profile import ClientProfilefrom tencentcloud.common.profile.http_profile import HttpProfilefrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptionfrom tencentcloud.ssl.v20191205 import ssl_client, modelsstart = time()# 请使用环境变量获取。不要硬编码secretId = os.getenv('TENCENT_CLOUD_SECRET_ID')secretKey = os.getenv('TENCENT_CLOUD_SECRET_KEY')cred = credential.Credential(secretId, secretKey)httpProfile = HttpProfile()httpProfile.endpoint = "ssl.tencentcloudapi.com"clientProfile = ClientProfile()clientProfile.httpProfile = httpProfiledomain_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')

四、结果展示

1. 申请批量证书。如下:


2. 下载证书内容。如下:


赞 (0)
上一篇 4天前
下一篇 4天前