Python3实现程序更新
发布日期:2021-05-04 15:51:17 浏览次数:12 分类:技术文章

本文共 3791 字,大约阅读时间需要 12 分钟。

很多软件都需要更新,而如何才可以让前台的程序受后台控制,可以更新呢?那么,今天就来教大家如何编写程序的更新程序。

搭建一个本地服务器

可以直接在cmd里cd到指定目录,然后py -m http.server 80。

创建好后,创建一个test.txt,先将其为空。
接着,放置一个xxx.exe。

代码走起

代码:

# coding:utf-8import urllibimport chardettry:    from urllib import request    response = request.urlopen("http://localhost/test.txt") #在这里的test.txt里注意填写的是最新版本的程序的下载链接。例如:http://localhost:5000/1.exe    html = response.read()    # charset = chardet.detect(html)  # 查看编码类型    # print(charset)    html = html.decode('utf-8')  # 对二进制码进行解码    if html == "":        exit()    else:        print("检测到当前版本过旧,有新版本啦!")        print("正在更新,请稍等哦~")        import os        try:                        url = html            filename = url[url.rindex('/') + 1:]  # 截取文件名            print('filename = ' + filename)                        print("正在下载!")                        downloaded = '0'            def download_listener(a, b, c):                per = 100.0 * a * b / c                if per > 100:                    per = 100                new_downloaded = '%.1f' % per                global downloaded                if new_downloaded != downloaded:                    downloaded = new_downloaded                    print('download %s%%  %s/%s' % (downloaded, a * b, c))            path = "C:/" #你可以自己设置            if not os.path.exists(path):                os.mkdir(path)            response = urllib.request.urlretrieve(url, path + filename, download_listener)            os.system("start C:/xxx.exe")   #运行新版本程序            print("下载完成!正在关闭旧版本程序!")            exit()        except:            print("啊哦~出错啦!你的网络貌似不佳哦~")except:    print("出错啦!")

运行后,发现:唉,怎么退出啦?

仔细观察,里面有:

if html == "":	exit()

因为test.txt等于空,所以程序退出了。

那么,怎么才能让它正确的运行呢?

修改test.txt

修改test.txt为xxx.exe的链接,例如:

http://localhost/xxx.exe/
请务必填写上http头部,不然,就会出错。
接着,运行程序。

成功

程序运行成功,并且下载了xxx.exe到C:/下,然后,还运行了xxx.exe。并且退出了程序。

如何在后台控制前台程序?

你需要将内网的localhost映射到外网。

你可以试试花生壳等产品。
我用花生壳映射一个域名:myweb.imwork.net
那么,我们的代码就可以改为:

# coding:utf-8import urllibimport chardettry:    from urllib import request    response = request.urlopen("http://myweb.imwork.net/test.txt") #在这里的test.txt里注意填写的是最新版本的程序的下载链接。例如:http://localhost:5000/1.exe    html = response.read()    # charset = chardet.detect(html)  # 查看编码类型    # print(charset)    html = html.decode('utf-8')  # 对二进制码进行解码    if html == "":        exit()    else:        print("检测到当前版本过旧,有新版本啦!")        print("正在更新,请稍等哦~")        import os        try:                        url = html            filename = url[url.rindex('/') + 1:]  # 截取文件名            print('filename = ' + filename)                        print("正在下载!")                        downloaded = '0'            def download_listener(a, b, c):                per = 100.0 * a * b / c                if per > 100:                    per = 100                new_downloaded = '%.1f' % per                global downloaded                if new_downloaded != downloaded:                    downloaded = new_downloaded                    print('download %s%%  %s/%s' % (downloaded, a * b, c))            path = "C:/" #你可以自己设置            if not os.path.exists(path):                os.mkdir(path)            response = urllib.request.urlretrieve(url, path + filename, download_listener)            os.system("start C:/xxx.exe")   #运行新版本程序            print("下载完成!正在关闭旧版本程序!")            exit()        except:            print("啊哦~出错啦!你的网络貌似不佳哦~")except:    print("出错啦!")

这样,程序就会爬取那个txt文件的信息,接着,下载文件到C盘,并更新了。

但是你需要确保你的网站有足够的流量进行下载操作,不然,可能会导致你的网站流量超出!

改进程序——支持删除旧程序

如何支持删除旧程序?那就需要新程序的帮忙了。

新程序运行后,先检查全盘中有没有指定的特殊文件,例如xxx.exe.detailedinformation,如果有,那么就获取那个文件的目录,然后截取前面的内容,加上“xxx.exe”,然后,删除那个旧的xxx.exe。
此处,因为太简单了,本人就不贴代码了。大家自己看看吧,特别简单。

如何应对程序名称被更改?

那可能就需要结合内容了。你可以在全盘(除了Windows目录)进行读取,读取文件内容,如果里面包含一些主要的东西,那么,判定这个文件就是旧程序。也许你需要截取你的应用程序的部分特征,才可以识别。

或者,你可以给你的应用程序“加花”,自己写一个特殊的花指令。如果检测到这种花指令,那么,就判定。

上一篇:Python从基础到进阶——tiantian520 著(转载)
下一篇:python3.7如何写自己的包(模块)

发表评论

最新留言

不错!
[***.144.177.141]2025年04月01日 14时49分48秒