import io from datetime import datetime from paramiko import SSHClient from scp import SCPClient from app import Paper, C, db from time import sleep html_text = '''

截止{time}
这里有{nc}门课程的{np}份资料
欢迎继续添砖加瓦

''' local_html_text = '''

目前这里有{nc}门课程的{np}份资料
欢迎继续添砖加瓦

''' def check_and_update(last_number): np = Paper.query.count() if np != last_number: nc = db.session.query(Paper.course.distinct()).count() t = datetime.now() username, host, path, target_host = C.wechat_html_address ssh = SSHClient() ssh.load_system_host_keys() ssh.connect(host, username=username) with open('static/login/count-info.html', 'w') as f: f.write(local_html_text.format(nc=nc, np=np, target_host=target_host)) with SCPClient(ssh.get_transport()) as scp: fl = io.BytesIO() fl.write(bytes(html_text.format(time=t.strftime("%Y-%m-%d %H:%M"), nc=nc, np=np, target_host=target_host), encoding='utf-8')) fl.seek(0) scp.putfo(fl, path) return np n = check_and_update(0) while True: try: print(str(datetime.now()), 'start ------') n = check_and_update(n) print(n) except: print('scp failed') sleep(300)