2011-04-26 192 views
2

在为我的程序创建自动更新程序的过程中,我无法成功下载.exe文件。Python - 从互联网下载.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 

我没有遇到错误,同时下载,但是当我尝试运行的执行,我收到以下错误:

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.

我自己上传执行,并且之前工作正常。

我也尝试了一些其他的下载方法,我发现,这导致了相同的错误,并且我也尝试上传到不同的网站以确保它不是这样。

有没有特殊的方法,我需要做到这一点?

编辑:

我做了一些进一步的测试与下载。我在另一台计算机上运行了该程序(我正在使用Spencer发布的内容) - 一个32位系统。 (Mine是一个64位)。我没有在那台计算机上看到错误,但是当我运行该程序时,命令行出现了,因为它是一个命令行样式.exe,我用它作为我的测试下载,但闪烁的白色入口栏的东西只是在我必须结束程序之前反弹到所有地方,所以显然有一些东西被损坏。

此外,下载过程可能与批处理文件?这将会更容易,因为程序将不得不重新开始使用新的更新,因为它使用的是全新的.exe。 (我要使用py2exe为使该计划的.exe)。

+1

由于文件扩展名是'.exe',我假设你在Windows上这样做。尝试将'f2'文件的模式更改为''wb''以二进制模式写入。默认是文本模式,同时会将遇到的所有换行符(字节)更改为回车+换行符。 – martineau 2011-04-26 22:51:37

+0

如果您手动下载(通过网页浏览器访问它)并执行它,您是否面临同样的问题? – 2011-04-26 22:58:54

+0

@Senthil它工作正常,如果我手动下载它。 – 2011-04-27 01:14:32

回答

2

根据官方Python文档为的urllib:

One caveat: the read() method, if the size argument is omitted or negative, may not read until the end of the data stream; there is no good way to determine that the entire stream from a socket has been read in the general case.

来自同一库的替代将是

import urllib 

url = '--insert-url--' 

filename = 'download.exe' 
urllib.urlretrieve(url, filename) 
+0

...非常有趣,我没有意识到这一点。如果这成为解决方案,请在这里留言,以便我注意到它。 – 2011-04-27 05:22:37

+0

它似乎加快了下载速度,但它仍然无法正常工作。 – 2011-04-27 19:59:19

3

我怀疑你需要在您的通话b(二进制)标志open:也

 
import urllib 

url = '--insert-url--' 

f = urllib.urlopen(url) 
file = f.read() 
f.close() 
f2 = open('download.exe', 'wb') 
f2.write(file) 
f2.close() 

,你在您拨打.close()时忽略了父母。不知道这是你的理解还是你的例子的问题,但我已经在上面的代码中修复了它。另外,如果您的.exe很大,您可能需要在下载文件时将其写入文件(目前您正在将整个内容读入内存)。这看起来是这样的:

f2 = open("download.exe", "wb") 
try: 
    while True: 
     data = f.read(4096) 
     if not data: 
      break 
     f2.write(data) 
finally: 
    f.close() 
    f2.close() 
+1

似乎没有解决它。 – 2011-04-26 22:53:45

+0

这很奇怪。你能否将使用Python下载的'.exe'分解到“working”'.exe'来查看差异? (我不使用Windows,所以我不能推荐diff工具...但是我相信Google会有一些建议) – 2011-04-26 22:56:48

+0

我使用了WinDiff,唯一的区别是它们有不同的创建日期。 – 2011-04-26 23:32:16

0

所以,我想,也许你有一个不同的问题。

My specs: Python3.X, installed via Homebrew. Using Python's urllib.request module, as it is the currently supported one.

我认为你正在下载一个html页面,它将你重定向到下载链接。如果您尝试从链接下载,则尤其如此。许多网站和服务器,你需要点击一个按钮,这将提供一个不同的URL从下载。例如,如果您尝试下载任何Microsoft link(如FCIV校验和程序),则下载按钮实际上会通过不同的URL路由您。


回答

我的建议是,你加载你在原来的答复为热媒/ L文件下载字节的文件。从这里开始,您可以尝试使用应用程序扩展来查找url;

举例来说,如果你坚持使用FCIV例子,你会下载,让相同的错误的东西:

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.

在仔细检查,如果您加载此文件作为了一个的.htm/L文件,你可以搜索x86字符串,并发现它在一个实际名称为https://download.microsoft.com/download/c/f/4/cf454ae0-a4bb-4123-8333-a1b6737712f7/Windows-KB841290-x86-ENU.exe的网址上。之后,如果你尝试使用这个新的url/https请求在OP中下载,你实际上会得到一个正确的.exe。


编辑对不起,这个答案可能只适用于在Python3.X 2017年答案是有点晚了原来的问题问6年前。另外,有关文件编写中b标志的其他答案和评论是正确的。该文件应该以wb权限打开。

相关问题