程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Use Python to call cloud API to apply for free certificates in batches and download them locally

編輯:Python

Usually we need to apply for a lot of ssl certificate , For example, the primary domain name ,cos Origin domain name ,cos Speed up the domain name and so on It's usually a little troublesome to apply one by one So if your domain name is dns Resolve in dnspod Words , You can use the following code to apply in batch

0. preparation

Please use this code first Child users create And authorize cloud API And SSL Certificate full permissions

Please note that To ensure the security of your account and cloud assets Please keep it carefully SecretId And SecretKey And regularly update Delete useless permissions

Go to create sub user :https://console.cloud.tencent.com/cam

1.SDK download

Please make sure Python Version is 3.6+

see Python edition

python3 -V

Upgrade pip, Install Tencent cloud Python SDK

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

2. Code section

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(' The domain name to apply for the certificate :')
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(' domain name :{0} The information has been submitted , Automatic verification after five seconds '.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(' domain name :{0} Verify success ! Ready to download certificate '.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(' This code execution takes a total of :', round(end - start, 2), 's')

If you have any questions, please leave a message in the comments section , Your favorite collection and attention will be the greatest motivation for the author to create

This article was first published on 2022.1.29 18:20

This article is cloud + Community first

Reprint is prohibited without consent

This article will be published synchronously in csdn You know Bili Bili column


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved