Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #56432 > unrolled thread
| Started by | Andreas Perstinger <andipersti@gmail.com> |
|---|---|
| First post | 2013-10-08 19:11 +0200 |
| Last post | 2013-10-08 19:11 +0200 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: parsing email from stdin Andreas Perstinger <andipersti@gmail.com> - 2013-10-08 19:11 +0200
| From | Andreas Perstinger <andipersti@gmail.com> |
|---|---|
| Date | 2013-10-08 19:11 +0200 |
| Subject | Re: parsing email from stdin |
| Message-ID | <mailman.868.1381252329.18130.python-list@python.org> |
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" <someone@example.com>
To: "me" <me@example.com>
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" <someone@example.com>
to: "me" <me@example.com>
subject: something
body: foo bar AÄÖÜĎӅ baz
Bye, Andreas
Back to top | Article view | comp.lang.python
csiph-web