
https://github.com/zjuchenyuan/EasyLogin/blob/master/examples/upyun/upyun.py
基于我的 EasyLogin 写的,其实就是封装了一下 requests 和 Beautifulsoup,提供了 cookie 保存和内容缓存机制
这个 upyun.py 会登录 console.upyun.com ,提供规则刷新和 https 证书更新功能,用法如
# 这里的 @会被替换成*, 刷新 https://py3.io/*.html 的缓存 python3 upyun.py purge https://py3.io/@.html # 查看所有证书,只显示当前正在使用的自有证书+正在使用的已经过期的证书 python3 upyun.py https list # 更新所有 30 天之内过期的证书 python3 upyun.py https renew 更新证书除了需要在 config.py 中提供登录的USERNAME和PASSWORD之外,还需要提供一个 api_func(domain_name)函数,这个函数需要返回证书信息
{"certificate":"-----BEGIN CERTIFICATE-----\n...", "private_key":"-----BEGIN RSA PRIVATE KEY-----\n..."} 我是使用函数计算用 dns 验证自动更新证书: https://py3.io/Nginx/#https_1
这个函数就是这个样子:(注意到传入的域名可能是子域名 需要判断后缀)
def renew_api(domain): namedict = { 'py3.io': 'py3io_ATxx', } for k, v in namedict.items(): if domain.endswith(k): return get_from_oss(v) return False import requests sess = requests.session() def get_from_oss(name): crt = sess.get("https://OSSNAME.oss-cn-REGION.aliyuncs.com/{name}.crt".format(name=name), headers={"Referer":"Referer_STRING"}) key = sess.get("https://OSSNAME.oss-cn-REGION.aliyuncs.com/{name}.key".format(name=name), headers={"Referer":"Referer_STRING"}) return {"certificate": crt.text, "private_key": key.text} 另外 upyun 真的会在 30 天删访问日志,然而法律要求至少保留六个月┑( ̄Д  ̄)┍ 所以需要自己下载日志咯: https://github.com/zjuchenyuan/EasyLogin/blob/master/examples/upyun/downloadlogs.py
从 config.py 读取 mydomains,调用 upyun 提供的 openapi 获取日志并下载