Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'received:192.168.178': 0.07; 'sys': 0.07; 'utf-8': 0.07; 'ascii': 0.09; 'data):': 0.09; 'msg': 0.09; 'subject:parsing': 0.09; 'def': 0.12; 'accepts': 0.16; 'andreas': 0.16; 'bye,': 0.16; 'feed(self,': 0.16; 'received:192.168.178.20': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'unicode': 0.24; 'looks': 0.24; "i've": 0.25; 'this:': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'work.': 0.31; 'code': 0.31; "skip:' 10": 0.31; 'work:': 0.31; 'file': 0.32; 'class': 0.32; 'skip:m 30': 0.32; 'text': 0.33; 'url:python': 0.33; 'skip:# 10': 0.33; 'subject:from': 0.34; "can't": 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'doubt': 0.36; 'url:org': 0.36; 'message-id:@gmail.com': 0.38; 'feed': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'subject:': 0.39; 'to:addr:python.org': 0.39; 'mentioned': 0.61; 'url:3': 0.61; 'news': 0.67; 'mail,': 0.68; 'body:': 0.84; 'pardon': 0.84; '8bit': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=T/gWUzgXZPBD6+Wy/9tPcecvJaVhaxNTEc41/RFv15A=; b=KwJmD2aWMdLAZLkZV9+Ft6zfPvY6gZjxFb5aKH0p8QW5QnI6uQ3uHgl/EH8b8kj7T8 Bzg/Pb7HwUabmLkkHB+nDUirZ5iuSO5S9R39xC5l8Iy0zwSpdPVq/ljix01dE0evBMEi foOk5nVk2/yu3U420qfqZEWj/GRBWfm26YKiSxiK3F8S173eLBBOFZCClbeF4iSZ4I88 N03jh80lw64k9WuhqXB1yQ8qJOfcrF1etgkL1UoXZ7vf62LevJyRpdTCkhfRfbR6DE28 EifV8EUCrjJGaCfZbJxNouHNO/azyIKZuglgIGPzPzLkFEO5LUB2CYaHVIzYTGF1p2CP cApQ== X-Received: by 10.204.78.8 with SMTP id i8mr2648839bkk.3.1381252321808; Tue, 08 Oct 2013 10:12:01 -0700 (PDT) Date: Tue, 08 Oct 2013 19:11:59 +0200 From: Andreas Perstinger User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: parsing email from stdin References: <5253F884.7020306@rece.vub.ac.be> <525415B4.1030208@gmail.com> <525423F4.7000902@rece.vub.ac.be> In-Reply-To: <525423F4.7000902@rece.vub.ac.be> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381252329 news.xs4all.nl 15972 [2001:888:2000:d::a6]:42596 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56432 On 08.10.2013 17:25, Antoon Pardon wrote: > Op 08-10-13 16:24, Andreas Perstinger schreef: >> Looking at the docs, I've found there is also "message_from_binary_file" >> which works for me with your code. >> >> http://docs.python.org/3/library/email.parser.html#email.message_from_binary_file >> > > I can't try that out right now, but I had a look at the code and the > ByteParser that is mentioned their looks like this: > > class BytesFeedParser(FeedParser): > """Like FeedParser, but feed accepts bytes.""" > > def feed(self, data): > super().feed(data.decode('ascii', 'surrogateescape')) > > > Somehow I doubt that trying to decode my utf-8 stream as if it was > ascii will work. Actually it does work: $ cat testmail.txt From: "someone" To: "me" Subject: something Content-Type: text/plain; charset="UTF-8"; Content-Transfer-Encoding: 8bit foo bar AÄÖÜĎӅ baz $ file testmail.txt testmail.txt: news or mail, UTF-8 Unicode text $ cat foo.py #!/usr/bin/python3 import sys from email import message_from_binary_file sys.stdin = sys.stdin.detach() msg = message_from_binary_file(sys.stdin) print("from: ", msg['From']) print("to: ", msg['To']) print("subject: ", msg['Subject']) print("body: ", msg.get_payload()) $ ./foo.py < testmail.txt from: "someone" to: "me" subject: something body: foo bar AÄÖÜĎӅ baz Bye, Andreas