3

让我们执行脚本

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 是否可能知道这个“原始”字符串是由我手动编辑的?

这里发生了非常偷偷摸摸的事情。

4

1 回答 1

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'
于 2013-07-26T04:53:29.340 回答