Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98596

Re: corrupt download with urllib2

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: corrupt download with urllib2
Date Tue, 10 Nov 2015 14:20:29 +0100
Organization None
Lines 51
Message-ID <mailman.209.1447161643.16136.python-list@python.org> (permalink)
References <n1sq82$k3g$1@news2.informatik.uni-stuttgart.de>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de KPvbl5p6OXF7TXJE4Vvy6gAFUo5rUXuHeExxD1Wyp5SQ==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'binary': 0.05; 'bytes.': 0.07; 'debugging.': 0.07; 'urllib2': 0.07; "%s'": 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'req': 0.09; 'python': 0.10; '2.7': 0.13; 'size,': 0.13; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:download': 0.16; 'wrote:': 0.16; 'later': 0.16; 'translation': 0.16; 'linux,': 0.18; 'try:': 0.18; 'windows': 0.20; 'exceptions': 0.22; 'import': 0.24; 'downloaded': 0.24; 'url:download': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'linux': 0.26; 'developing': 0.28; 'skip:u 20': 0.28; 'skip:( 20': 0.28; 'perl': 0.29; 'code:': 0.29; 'compiled': 0.32; 'run': 0.33; 'windows.': 0.33; 'open': 0.33; 'file': 0.34; 'except': 0.34; 'server': 0.34; 'skip:p 30': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'mode': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'wrong': 0.38; 'skip:p 20': 0.38; 'to:addr:python.org': 0.40; 'where': 0.40; 'subject:with': 0.40; 'received:de': 0.40; 'skip:u 10': 0.61; 'avoid': 0.61; 'therefore': 0.67
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd8e07.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:98596

Show key headers only | View raw


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()))
> 

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

corrupt download with urllib2 Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-10 13:08 +0000
  Re: corrupt download with urllib2 Peter Otten <__peter__@web.de> - 2015-11-10 14:20 +0100
    Re: corrupt download with urllib2 Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-10 13:43 +0000
  Re: corrupt download with urllib2 Peter Otten <__peter__@web.de> - 2015-11-10 14:40 +0100
    Re: corrupt download with urllib2 Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-10 13:59 +0000
      Re: corrupt download with urllib2 Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-10 15:51 +0000
        Re: corrupt download with urllib2 Peter Otten <__peter__@web.de> - 2015-11-10 17:48 +0100
          Re: corrupt download with urllib2 Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-10 17:21 +0000

csiph-web