corrupt download with urllib2
Peter Otten
__peter__ at web.de
Tue Nov 10 08:20:29 EST 2015
Ulli Horlacher wrote:
> I am currently developing a program which should run on Linux and Windows.
> Later it shall be compiled with PyInstaller. Therefore I am using Python
> 2.7
>
> My program must download http://fex.belwue.de/download/7za.exe
>
> I am using this code:
> It works with Linux, but not with Windows 7, where the downloaded 7za.exe
> is corrupt: it has the wrong size, 589044 instead of 587776 Bytes.
>
> Where is my error?
> sz = path.join(fexhome,'7za.exe')
> szurl = "http://fex.belwue.de/download/7za.exe"
>
> try:
> szo = open(sz,'w')
Open the file in binary mode to avoid the translation of "\n" into "\r\n":
szo = open(sz, 'wb')
> except (IOError,OSError) as e:
> die('cannot write %s - %s' % (sz,e.strerror))
Unrelated, but I recommend that you let the exceptions bubble up for easier
debugging.
Python is not Perl ;)
> import urllib2
> printf("\ndownloading %s\n",szurl)
> try:
> req = urllib2.Request(szurl)
> req.add_header('User-Agent',useragent)
> u = urllib2.urlopen(req)
> except urllib2.URLError as e:
> die('cannot get %s - %s' % (szurl,e.reason))
> except urllib2.HTTPError as e:
> die('cannot get %s - server reply: %d %s' % (szurl,e.code,e.reason))
> if u.getcode() == 200:
> print(u.read(),file=szo,end='')
> szo.close()
> else:
> die('cannot get %s - server reply: %d' % (szurl,u.getcode()))
>
More information about the Python-list
mailing list