详细场景如下:定时监听接口,接口根据传来的version值与当前版本对比,如果版本号一致,则继续监听,否则下载更新并执行一个shell脚本。
主要使用了urllib库中request模块完成。
详细代码如下:
from threading import Timer
import urllib.request
import json
import os
def hello():
print("hello hello")
t = Timer(2, qaq) # 八个小时为28800
t.start()
def cbk(a, b, c):
"""回调函数
@a:已经下载的数据块
@b:数据块的大小
@c:远程文件的大小
"""
per = 100.0*a*b/c
if per > 100:
per = 100
print('%.2f%%' % per)
def compare_version(data):
url = 'http://www.flasktest.com/version'
# response = urllib.request.Request(url)
# html = urllib.request.urlopen(response)
data_encode = urllib.parse.urlencode(data).encode('utf-8')
response = urllib.request.Request(url, data_encode) # 获取返回值
html = urllib.request.urlopen(response)
print(html.getcode())
msg = html.read().decode()
print(msg)
msg = json.loads(msg)
print(type(msg['version_now']))
if msg['re