# Update Certificate from Server NginxProxyManager import ctypes import os import sys import time import zipfile import requests server_url = 'https://yourdomain.com' certificate_index = 1 npm_username = '' npm_password = '' login_url = f'{server_url}/api/tokens' download_url = f'{server_url}/api/nginx/certificates/{certificate_index}/download' def login(): r = requests.post(url=login_url, json={"identity": npm_username, "secret": npm_password}) print(r.status_code, r.text) if r.status_code == 200: print('登录成功,准备下载证书文件') download_certificates(r.json()['token']) else: print('登录失败') def download_certificates(token): r = requests.get(url=download_url, headers={'Authorization': 'Bearer ' + token}) print(r.url, r.status_code) if r.status_code == 200: content = r.content with open('certificates.zip', 'wb') as file: file.write(content) file.close() print('证书下载成功!') unzip_certificates('certificates.zip') else: print('证书下载失败!', r.text) def unzip_certificates(file): zip_file = zipfile.ZipFile(file) zip_file.extractall(path='./letsencrypt/live/') print('证书文件已解压,请手动更新配置文件以使用新证书,并重启Nginx') # restart_nginx() def restart_nginx(): a = os.system('sc stop Nginx') print(a) time.sleep(1) a = os.system('sc start Nginx') print(a) print('Nginx 服务重启成功') # https://www.jinky.top/posts/request-uac-using-python/ def request_uac(): if not is_admin(): # Re-run the program with admin rights ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1) def is_admin(): try: return ctypes.windll.shell32.IsUserAnAdmin() except: return False # Press the green button in the gutter to run the script. if __name__ == '__main__': request_uac() login() # See PyCharm help at https://www.jetbrains.com/help/pycharm/