python程序打包成exe实现新版本的自动更新检测及下载

这篇具有很好参考价值的文章主要介绍了python程序打包成exe实现新版本的自动更新检测及下载。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

python使用pyinstaller打包成的exe程序,代码修改重新打包就需要重新发送一次程序,略微麻烦,通过服务器存储新版本打包后的程序,检测和下载通过代码实现。
本文通过FTP局域网服务器的形式完成,使用serv-u软件配置FTP服务器,配置方式可移步下方站内链接
Serv-U配置FTP服务器
使用designer画一个简单的ui,如下图
python自动检测插件的最新版,python,exe,python,ui,开发语言
转成py文件如下,命名为 update_test_ui.py代码如下

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'update_test.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainTestUp(object):
    def setupUi(self, MainTestUp):
        MainTestUp.setObjectName("MainTestUp")
        MainTestUp.resize(603, 378)
        self.centralwidget = QtWidgets.QWidget(MainTestUp)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(220, 150, 161, 61))
        font = QtGui.QFont()
        font.setPointSize(20)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setStyleSheet("background-color: rgb(0, 255, 255);")
        self.pushButton.setObjectName("pushButton")
        MainTestUp.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainTestUp)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 603, 23))
        self.menubar.setObjectName("menubar")
        MainTestUp.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainTestUp)
        self.statusbar.setObjectName("statusbar")
        MainTestUp.setStatusBar(self.statusbar)

        self.retranslateUi(MainTestUp)
        self.pushButton.clicked.connect(MainTestUp.close) # type: ignore
        QtCore.QMetaObject.connectSlotsByName(MainTestUp)

    def retranslateUi(self, MainTestUp):
        _translate = QtCore.QCoreApplication.translate
        MainTestUp.setWindowTitle(_translate("MainTestUp", "软件自升级测试"))
        self.pushButton.setText(_translate("MainTestUp", "确定"))

定义一个main.py调用ui的py文件,并添加版本检测、FTP的服务器登录和文件下载功能,代码如下

# -*- coding:utf-8 -*-
from PyQt5.Qt import *
import sys
import os
import socket
from ftplib import FTP
import update_test_ui


class UpdateTest(update_test_ui.Ui_MainTestUp, QMainWindow):
    def __init__(self):
        super(update_test_ui.Ui_MainTestUp, self).__init__()
        super().__init__()
        self.setupUi(self)
        self.current_version = 'v2'
        # 承接服务器的文件名称
        self.remote_name = ''
        # 承接远程服务器文件路径
        self.remote_path = ''
        # 当前exe程序地址
        # self.local_folder = os.path.abspath(os.path.dirname(__file__))
        # 运行第一步检查是否要更新
        self.first_step()

    def first_step(self):
        try:
            # 登录FTP
            self.my_ftp = self.login_ftp("sq", "1")
            print(self.my_ftp.welcome)
            # 服务器文件目录
            self.remote_path = self.getfile("/suite")
            # 服务器文件名称
            self.remote_name = self.remote_path.split("/")[-1]
            # 服务器文件版本(设置服务器文件名称格式示例:软件名称_v1.exe)
            remote_version = self.remote_name.split("_")[-1].split(".")[0]
            # 判断服务器软件版本是否与本地版本一致
            if self.current_version != remote_version:
                # 本地路径,程序所在文件夹的上一级目录
                local_path = os.path.abspath(os.path.join(os.getcwd(), "../."))
                # 拼接一个本地路径的文件
                local_file = local_path + "/" + self.remote_name
                # 如果服务器文件还未下载
                if not os.path.exists(local_file):
                    # 本地文件目录全地址
                    local_full = local_path + "/" + self.remote_name
                    # 执行下载
                    self.download_file(local_full, self.remote_path)
                    QMessageBox.warning(self, "提示", "最新版软件已下载,请在上一级目录安装使用")
                # 服务器文件已下载
                else:
                    # 本地已存在,请在上一级目录安装使用最新版软件
                    QMessageBox.warning(self, "提示", "请在上一级目录安装使用最新版软件")
                # 设置下一步按钮不可操作,可强制使其使用新版本软件
                self.pushButton.setEnabled(False)
            else:
                pass
        except Exception:
            pass

    # 登录FTP服务器
    def login_ftp(self, username, password):
        try:
            timeout = 60
            socket.setdefaulttimeout(timeout)
            ftp = FTP()
            # 0主动模式 1 #被动模式
            ftp.set_pasv(False)
            host = "127.0.0.1"
            port = 21
            ftp.encoding = 'utf-8'  # 'gbk'
            ftp.connect(host, port)
            ftp.login(username, password)
            return ftp
        except Exception:
            pass

    # 获得FTP目录及子目录下的文件名称
    @staticmethod
    def get_file_name(f):
        names = f.split()
        if len(names) < 9:
            return ''
        else:
            file_names = names[8:]
            res = ''
            for name in file_names:
                res = res + name + ''
            return res.strip()

    # 获得FTP目录及子目录下的文件
    def getfile(self, path):
        self.my_ftp.cwd(path)
        file_list = []
        self.my_ftp.retrlines("LIST", file_list.append)
        for f in file_list:
            if f.startswith("d"):
                file_name = self.get_file_name(f)
                if file_name == '.' or file_name == '..':
                    continue
                path_a = self.my_ftp.pwd() + "/" + str(f).split(' ')[-1]
                self.getfile(path_a)
                self.my_ftp.cwd("..")
            else:
                if len(str(f).split(' ')) > 3:
                    # 得到文件名,并按照指定语句输出
                    a = self.my_ftp.pwd() + "/" + str(f).split(' ')[-1]
                    return a

    # 判断本地是否已经存在相同文件
    def is_same_size(self, local_file, remote_file):
        try:
            remote_file_size = self.my_ftp.size(remote_file)
        except Exception:
            remote_file_size = -1
        try:
            local_file_size = os.path.getsize(local_file)
        except Exception:
            local_file_size = -1
        if remote_file_size == local_file_size:
            return 1
        else:
            return 0

    def download_file(self, local_file, remote_file):
        if self.is_same_size(local_file, remote_file):
            return
        else:
            try:
                buf_size = 1024
                file_handler = open(local_file, 'wb')
                self.my_ftp.retrbinary('RETR %s' % remote_file, file_handler.write, buf_size)
                file_handler.close()
            except Exception:
                return


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = UpdateTest()
    ui.show()
    sys.exit(app.exec())

代码还未完全实现自动化,站内有通过.bat进行自动化下载及安装的,对.bat文件的运行规则不懂,尝试未成功,文中的方法还需一步处理才能使用新版软件,且该方法让确定按钮不可点击,必须更新才可使用,若有大佬有更厉害的更加自动化的方法,请留下脚步。文章来源地址https://www.toymoban.com/news/detail-626784.html

到了这里,关于python程序打包成exe实现新版本的自动更新检测及下载的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Python文件打包exe程序

    脚本打包exe:win/mac【终端】 qt5,开发桌面应用 打包qt5程序【桌面应用】 注意事项: 支持mac、win(Windows建议使用python3.6.8) 配合虚拟环境打包 过程详解: 1、pyinstaller包:帮我们快速打包应用程序! 2、Windows建议使用python3.6.8:对程序打包会发生更少的bug! 3、建议配合虚拟环

    2024年02月08日
    浏览(33)
  • 【Python】项目打包:如何使用PyInstaller打包python程序(exe)

    常用python的开发者现在也是很多的,用python可以做很多事情,如果涉及到python桌面开发一定会使用PyInstaller将程序打包成 执行程序 ,如果要求更高的话还会再次封装成 安装程序 (工具inno setup)。 执行程序就是复制到其他电脑可以直接运行,不需要安装配置python环境。 安装

    2024年02月13日
    浏览(37)
  • Python PyInstaller将程序打包为exe程序

    1.执行 pip install pyinstaller,结果如下图  2.然后执行 pyinstaller -F -w Hello.py 执行完之后会产生两个目录 在dist目录下就是可执行文件,双击即可看到效果

    2024年02月04日
    浏览(36)
  • 将Python程序打包成exe文件

    我新写了一篇更加完整的文章,与这篇文章相比, 它新增了两种打包方式:多python文件打包和含有资源文件的打包方式 ,具体请戳链接: 用 Pyinstaller 模块将 Python 程序打包成 exe 文件(全网最全面最详细)_小康2022的博客-CSDN博客 本文一步一步地教你如何用 Pyinstaller 模块将

    2024年02月03日
    浏览(37)
  • mac系统python程序打包成exe,mac系统怎么打包python

    本篇文章给大家谈谈python打包成可执行文件 mac,以及mac系统python程序打包成exe,希望对各位有所帮助,不要忘了收藏本站喔。 要将Python代码打包成应用程序,你可以使用多种工具和方法。以下是两种比较常见的方法: 使用PyInstaller:PyInstaller是一个可将Python代码打包成独立可

    2024年02月22日
    浏览(37)
  • python使用Tkinter和打包exe程序

    链接 链接 这里不多说了,你们看他用的吧,我也是用了再看吧,我也就简单用个按钮而已 1、打包成多文件 打包的文件是多个文件的 dist中的整个文件夹都要发给你朋友 2、打包成单个文件 打包出来就一个exe文件在dist中 3、命名 4、加图片 5、查看更多 6、路径问题 如果你的

    2024年01月17日
    浏览(32)
  • python打包Windows.exe程序(pyinstaller)

    python打包Windows.exe程序(pyinstaller) pip install pyinstaller 使用pip命令来安装pyinstaller模块。 -F: pyinstaller -F hello.py -p hello2.py -D: pyinstaller -D hello.py -p hello2.py -i : pyinstaller -i tb.ico -F hello.py -p hello2.py 其中前一个文件hello是主文件,后一个文件是会被调用到的文件,可以有多个。

    2024年02月13日
    浏览(38)
  • 【Python】使用nuitka打包Python程序为EXE可执行程序

    1.说明 写好的Python程序如果想要拿到其他电脑上运行,那还得安装一下Python环境和各种库,这是比较麻烦的,所以有必要把它打包成一个可执行的exe文件。可以打包exe的库有好多个,比如说pyinstaller、cx_Freeze等。 pyinstaller打包比较简单,如果有需要可以参考之前的文章【Pyth

    2024年01月25日
    浏览(31)
  • 微信小程序启动自动检测版本更新,检测到新版本则提示更新

    UpdateManager 对象,用来管理更新,可通过 wx.getUpdateManager 接口获取实例 在app.js中的示例代码 UpdateManager.applyUpdate() 强制小程序重启并使用新版本。在小程序新版本下载完成后(即收到 onUpdateReady 回调)调用。 UpdateManager.onCheckForUpdate(function listener) 监听向微信后台请求检查更新

    2024年02月07日
    浏览(34)
  • 将Python打包为exe+inno setup将exe程序封装成向导安装程序

    为什么要打包? Python脚本不能在没有安装Python的机器上运行。如果写了一个脚本,想分享给其他人使用,可她电脑又没有装Python。如果将脚本打包成exe文件,即使她的电脑上没有安装Python解释器,这个exe程序也能在上面运行。 1、在pycharm中安装pyinstaller 2、进入你所在的文件

    2024年01月25日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包