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


Groups > comp.lang.python > #8767

Re: HeaderParseError

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
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; 'variant': 0.07; 'base64': 0.09; 'padding': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'output': 0.11; 'package.': 0.12; 'wrote:': 0.15; '"/"': 0.16; '(x,': 0.16; '42,': 0.16; 'anmeldung': 0.16; 'garbage.': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'thunderbird': 0.16; 'skip:" 40': 0.16; 'this:': 0.16; '>>>': 0.16; 'def': 0.16; '(most': 0.21; 'incorrect': 0.23; 'traceback': 0.25; 'url:wiki': 0.25; 'skip:b 20': 0.26; 'raise': 0.28; 'skip:" 30': 0.28; 'import': 0.29; 'module': 0.30; 'from:addr:web.de': 0.30; 'looks': 0.30; 'skip:b 30': 0.31; 'print': 0.32; 'shows': 0.32; 'does': 0.32; 'skip:" 20': 0.33; 'to:addr:python-list': 0.34; 'header:X-Complaints- To:1': 0.34; 'instead': 0.34; 'characters': 0.34; 'last):': 0.35; 'skip:" 50': 0.35; 'uses': 0.35; 'skip:" 10': 0.36; 'file': 0.36; 'page': 0.36; 'but': 0.37; 'could': 0.37; 'received:org': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'patch': 0.38; 'header:Mime- Version:1': 0.39; 'skip:e 20': 0.39; 'url:en': 0.39; 'to:addr:python.org': 0.39; 'peter': 0.62; 'encountered': 0.73; '101,': 0.84; 'otten': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: HeaderParseError
Date Mon, 04 Jul 2011 13:20:42 +0200
Organization None
References <97dc37F7gaU1@mid.individual.net> <ius2f9$e1$1@solani.org> <97djhfF10kU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p5084bc56.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.595.1309778458.1164.python-list@python.org> (permalink)
Lines 67
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1309778458 news.xs4all.nl 21890 [2001:888:2000:d::a6]:58725
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8767

Show key headers only | View raw


Thomas Guettler wrote:

> On 04.07.2011 11:51, Peter Otten wrote:
>> Thomas Guettler wrote:
>> 
>>> I get a HeaderParseError during decode_header(), but Thunderbird can
>>> display the name.
>>>
>>>>>> from email.header import decode_header
>>>>>>
>> 
decode_header('=?iso-8859-1?B?QW5tZWxkdW5nIE5ldHphbnNjaGx1c3MgU_xkcmluZzNwLmpwZw==?=')
>>> Traceback (most recent call last):
>>>   File "<stdin>", line 1, in <module>
>>>   File "/usr/lib64/python2.6/email/header.py", line 101, in
>>>   decode_header
>>>     raise HeaderParseError
>>> email.errors.HeaderParseError

>> The characters after "...Netzanschluss " look like garbage. What does
>> Thunderbird display?
> 
> Hi Peter, Thunderbird shows this:
> 
> Anmeldung Netzanschluss Südring3p.jpg

>>> a = u"Anmeldung Netzanschluss 
Südring3p.jpg".encode("iso-8859-1").encode("base64")

>>> b = "QW5tZWxkdW5nIE5ldHphbnNjaGx1c3MgU_xkcmluZzNwLmpwZw==?="
>>> for i, (x, y) in enumerate(zip(a, b)):
...     if x != y: print i, x, y
...
33 / _
52
?
>>> b.decode("base64")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/encodings/base64_codec.py", line 42, in 
base64_decode
    output = base64.decodestring(input)
  File "/usr/lib/python2.6/base64.py", line 321, in decodestring
    return binascii.a2b_base64(s)
binascii.Error: Incorrect padding
>>> b.replace("_", "/").decode("base64")
'Anmeldung Netzanschluss S\xfcdring3p.jpg'

Looks like you encountered a variant of base64 that uses "_" instead of "/" 
for chr(63). The wikipedia page http://en.wikipedia.org/wiki/Base64
calls that base64url.

You could try and make the email package accept that with a monkey patch 
like the following:

#untested
import binascii
def a2b_base64(s):
    return binascii.a2b_base64(s.replace("_", "/"))

from email import base64mime
base64mime.a2b_base64 = a2b_base64

Alternatively monkey-patch the binascii module before you import the email 
package.

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


Thread

HeaderParseError Thomas Guettler <hv@tbz-pariv.de> - 2011-07-04 10:31 +0200
  Re: HeaderParseError Peter Otten <__peter__@web.de> - 2011-07-04 11:51 +0200
    Re: HeaderParseError Thomas Guettler <hv@tbz-pariv.de> - 2011-07-04 12:38 +0200
      Re: HeaderParseError Peter Otten <__peter__@web.de> - 2011-07-04 13:20 +0200
        Re: HeaderParseError Thomas Guettler <hv@tbz-pariv.de> - 2011-07-05 14:02 +0200

csiph-web