Python:(msg = email.message_from_string(aaa))当尝试从原始电子邮件源解析东西时返回值(无)

时间:2013-07-26 04:46:27

标签: python python-2.7 mod-wsgi wsgi

让我们执行脚本

python b.wsgi

结果是:

None
None

这就是问题,这里是完整的脚本b.wsgi

aaa = """
From root@a1.local.tld Thu Jul 25 19:28:59 2013
Received: from a1.local.tld (localhost [127.0.0.1])
    by a1.local.tld (8.14.4/8.14.4) with ESMTP id r6Q2SxeQ003866
    for <ooo@a1.local.tld>; Thu, 25 Jul 2013 19:28:59 -0700
Received: (from root@localhost)
    by a1.local.tld (8.14.4/8.14.4/Submit) id r6Q2Sxbh003865;
    Thu, 25 Jul 2013 19:28:59 -0700
From: root@a1.local.tld
Subject: oooooooooooooooo
To: ooo@a1.local.tld
Cc: 
X-Originating-IP: 192.168.15.127
X-Mailer: Webmin 1.420
Message-Id: <1374805739.3861@a1>
Date: Thu, 25 Jul 2013 19:28:59 -0700 (PDT)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="bound1374805739"

This is a multi-part message in MIME format.

--bound1374805739
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

ooooooooooooooooooooooooooooooooooooooooooooooo

--bound1374805739--
"""

import email
msg = email.message_from_string(aaa)
print msg['From']
print msg['To']

我尝试将其更改为

print msg['from']
print msg['to']

同样的问题。

这可能是什么问题?

PYTHON是否可能知道这个“原始”字符串是由我的手工编辑的?

非常偷偷摸摸的东西在这里。

1 个答案:

答案 0 :(得分:5)

字符串开头和结尾的\n导致问题。试试这个

>>> msg = email.message_from_string(aaa.strip())
>>> msg.keys()
['Received', 'Received', 'From', 'Subject', 'To', 'Cc', 'X-Originating-IP', 'X-Mailer', 'Message-Id', 'Date', 'MIME-Version', 'Content-Type']
>>> msg['From']
'root@a1.local.tld'