Python - 从互联网下载 .exe 文件
在为我的程序创建自动更新程序的过程中,我无法成功下载 .exe 文件。
我所做的事情是这样的:
import urllib
url = '--insert-url--'
f = urllib.urlopen(url)
file = f.read()
f.close
f2 = open('download.exe', 'w')
f2.write(file)
f2.close
下载时没有遇到错误,但是当我尝试运行执行时,出现以下错误:
该文件的版本不是 与Windows版本兼容 你正在跑步。检查你的电脑 系统信息,看看您是否 需要 x86(32 位)或 x64(64 位) 程序的版本,然后 联系软件发行商。
我自己上传了执行结果,之前运行得很好。
我还尝试了一些我发现的其他下载方法,这导致了相同的错误,并且我还尝试上传到不同的网站以确保不是这样。
我需要采取特殊的方法来做到这一点吗?
编辑:
我对下载进行了一些进一步的测试。我在另一台计算机(32 位系统)上运行了该程序(我正在使用 Spencer 现在发布的内容)。 (我的是 64 位。)我在那台计算机上没有收到错误,但是当我运行该程序时,命令行出现,因为它是我用作我的命令行样式的 .exe测试下载,但是在我必须结束程序之前,闪烁的白色输入栏东西就到处跳动,所以显然有些东西被损坏了。
另外,可以使用批处理文件进行下载过程吗?这几乎会更容易,因为程序必须重新启动才能开始使用新的更新,因为它使用的是全新的 .exe。 (我将使用 py2exe 使程序成为 .exe。)
In the process of creating an auto-updater for my program, and I'm having trouble successfully downloading an .exe file.
What I was doing was something like this:
import urllib
url = '--insert-url--'
f = urllib.urlopen(url)
file = f.read()
f.close
f2 = open('download.exe', 'w')
f2.write(file)
f2.close
I encountered no errors while downloading, but when I try to run the execution, I get the following error:
The version of this file is not
compatable with the version of Windows
you're running. Check your computer's
system information to see whether you
need an x86(32-bit) or an x64 (64-bit)
version of the program, and then
contact the software publisher.
I uploaded the execution myself and it worked fine before.
I also tried some various other methods for downloading that I found, which resulted in the same error, and I also tried uploading to different sites to make sure that wasn't it.
Is there a special way I need to do this?
EDIT:
I did some further testing with the download. I ran the program (I'm using what Spencer posted now) on a different computer -- a 32-bit system. (Mine is a 64-bit.) I don't get the error on that computer, but when I run the program, the command line comes up, as it is a command-line style .exe that I'm using as my test download, but the blinking white entry bar thing just bounces all over the place before I have to end the program, so something is obviously getting corrupted.
Also, would the downloading process be possible with a Batch file? This would almost be easier as the program is going to have to restart to begin using the new update anyway, as it is using an entirely new .exe. (I'm going to use py2exe for making the program an .exe.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 urllib 的官方 python 文档:
来自同一个库的替代方案是
According to the official python docs for urllib:
an alternative from the same library would be
我怀疑您需要在对
open
的调用中包含b
(二进制)标志:此外,您在对
.close 的调用中省略了父级()
。不确定这是否是您的理解或示例的问题,但我已在上面的代码中修复了它。另外,如果您的
.exe
很大,您可能希望在下载时将其写入文件(当前您正在将整个文件读入内存)。那看起来像:I suspect that you need to include the
b
(binary) flag in your call toopen
:Also, you've omitted the parents in your calls to
.close()
. Not sure if that's a problem with your understanding or your example, but I've fixed it in the code above.Also, if your
.exe
is big, you may want to write it to the file as you download it (currently you're reading the entire thing into memory). That would look something like:所以,我想也许你遇到了不同的问题。
我认为您正在下载一个 html 页面,该页面将您重新路由到下载链接。如果您尝试从链接下载,尤其是这种情况。许多网站和服务器都会让您单击一个按钮,该按钮将提供不同的下载网址。
例如,如果您尝试下载任何 Microsoft 链接,与 FCIV 校验和程序一样,下载按钮实际上会通过不同的 url 引导您。
回答
我的建议是您将在原始答案中下载的字节文件加载为 .htm/l 文件。从这里,您可以尝试查找带有应用程序扩展名的网址;
例如,如果您坚持使用 FCIV 示例,您将下载一些出现相同错误的内容:
仔细检查后,如果将此文件作为 .htm/l 文件加载,则可以搜索 x86 字符串并发现它位于具有实际名称的 URL 上
https://download.microsoft.com/download /c/f/4/cf454ae0-a4bb-4123-8333-a1b6737712f7/Windows-KB841290-x86-ENU.exe
之后,如果您尝试使用这个新的 url/https 请求在 OP 中下载,您将实际上会有一个正确的.exe。编辑 抱歉,这个答案可能只适用于2017年的Python3.X。这个答案相对于6年前提出的原始问题来说有点晚了。此外,关于文件写入中的
b
标志的其他答案和注释是正确的。应使用wb
权限打开该文件。So, I think maybe you were having a different problem.
I think that you were downloading a html page, which re-routed you to the download link. This is especially the case if you are trying to download from link. Many websites, and servers, have you click a button, which will provide a different url to download from.
For instance, if you try to download any Microsoft link, like the FCIV checksum program, the download button actually routes you through a different url.
Answer
My suggestion is that you load up the byte file you downloaded in your original answer as a .htm/l file. From here, you can try to find a url with an application extension;
For instance, if you stick with the FCIV example, you will download something that give the same error:
On closer inspection, if you load this file up as a .htm/l file, you can search for the x86 string and discover that it is on a url with actual name
https://download.microsoft.com/download/c/f/4/cf454ae0-a4bb-4123-8333-a1b6737712f7/Windows-KB841290-x86-ENU.exe
After which, if you try your download in the OP with this new url/https request, you will actually have a .exe that is correct.EDIT Sorry, this answer may only apply to Python3.X in the year 2017. The answer is a little late to the original question asked 6 years ago. Also, the other answers and comments about the
b
flag in file writing is correct. The file should be opened withwb
permissions.