tkinter软件自动更新,多线程流下载

import base64
import os
import re
import threading
import tkinter
import tkinter.messagebox
import time
from tkinter import ttk
from Tools.tools import open_url

import requests
# url = '兰凑云文件地址'
# https://www.lanzoux.com/...
class Update:
    def __init__(self, url, banben):
        url = 'https://www.lanzous.com/tp/{}'.format(str(url).split('/')[-1])
        print(url)
        self.url = url
        self.banben = banben
        self.main()
    
    def Lanzou_geturl(self, url):
        while 1:
            try:
                self.lab['text'] = '正在加载'
                # 蓝奏云分享文件链接地址
                # header头,注意那个referer必须要与上面文件分享地址url相同
                headers = {
     
                    'host'      : 'www.lanzous.com',
                    'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
                }
                # 获取分享页面html文件
                res = requests.get(url, headers=headers)
                # print(res.text)
                edow = re.findall("domianload = '(.*?)'", res.text)
                nmousedow = re.findall("downloads = '(.*?)';", res.text)
                url2 = edow[0] + nmousedow[0]
                print(url2)
                self.lab['text'] = url2
                # 请求最终重定向地址
                headers2 = {
     
                    'host'           : 'vip.d2.baidupan.com',
                    'User-Agent'     : 'Mozilla/5.0 (Linux; U; Android 8.0.0; zh-cn; MI 6 Build/OPR1.170623.027) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/61.0.3163.128 Mobile Safari/537.36 XiaoMi/MiuiBrowser/10.5.1',
                    'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
                }
                res4 = requests.head(url2, headers=headers2)
                print(res4.headers['Location'])
                return res4.headers['Location']
            except:
                time.sleep(2)
    
    def down_file(self, url, filesize):
        try:
            buffer = 10240
            rsp = requests.get(url, stream=True)  # 流下载模式
            offset = 0
            for chunk in rsp.iter_content(chunk_size=buffer):
                if not chunk: break
                self.the_fileobj.seek(offset)  # 设置指针位置
                self.the_fileobj.write(chunk)  # 写入文件
                offset = offset + len(chunk)
                proess = offset / int(filesize) * 100
                self.lab['text'] = '进度---->{:.2f}%'.format(proess)
                # download_proess_signal.emit(int(proess))        #发送信号
            #######################################################################
            self.the_fileobj.close()  # 关闭文件
            # exit(0)  # 关闭线程
            tkinter.messagebox.showinfo('下载完毕!', '最新版软件已经下载完成!')
            os.startfile(os.getcwd())
            self.root.destroy()
        except Exception as e:
            print('sss', e)
    
    def start(self):
        down_url = self.Lanzou_geturl(self.url)
        the_filesize = requests.get(down_url, stream=True).headers['Content-Length']
        
        the_filepath = "{}/小白V{}.exe".format(os.getcwd(), self.banben)
        self.the_fileobj = open(the_filepath, 'wb')
        # 创建下载线程
        t = threading.Thread(target=self.down_file, args=(down_url, the_filesize))
        t.daemon = True
        t.start()
    
    def start_threading(self):
        t = threading.Thread(target=self.start)
        t.daemon = True
        t.start()
    
    def main(self):
        self.root = tkinter.Tk()
        self.root.title("更新助手")
        self.root.minsize(350, 80)
        self.root.geometry('300x70')
        self.root.resizable(width=False, height=False)
        
        # 状态标签
        self.lab = tkinter.Label(self.root, text='进度', fg='darkgreen', font=('微软雅黑', 12))
        self.lab.place(x=5, y=10, width=350, height=20)
        
        but = ttk.Button(self.root, text='下载文件', command=self.start_threading)
        but.place(x=10, y=40, width=100, height=30)
        
        but = ttk.Button(self.root, text='网页下载', command=lambda: open_url('兰凑云文件地址'))
        but.place(x=180, y=40, width=100, height=30)
        
        self.root.mainloop()

# 使用方式:
# Update('兰凑云文件地址', '1111')

你可能感兴趣的:(python笔记,tkinter,python,多线程)