从Python smtplib捕获调试输出
如何从Python smtplib库捕获调试输出 这是我的测试程序:从Python smtplib捕获调试输出,python,error-handling,smtplib,Python,Error Handling,Smtplib,如何从Python smtplib库捕获调试输出 这是我的测试程序: import smtplib s = smtplib.SMTP("mx10.comcast.com") s.set_debuglevel(1) s.sendmail("no-such-sender@comcast.com",["no-such-receiver@comcast.com"],""" from: no-such-sender@comcast.com to: no-such-receiver@comcast.com
import smtplib
s = smtplib.SMTP("mx10.comcast.com")
s.set_debuglevel(1)
s.sendmail("no-such-sender@comcast.com",["no-such-receiver@comcast.com"],"""
from: no-such-sender@comcast.com
to: no-such-receiver@comcast.com
subject: no such message
This message won't be delivered to anybody.
""")
以下是输出:
send: 'ehlo dance.local\r\n'
reply: '250-mx10.comcast.com says EHLO to 129.6.220.67:57015\r\n'
reply: '250-SIZE 40000000\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-8BITMIME\r\n'
reply: '250 XXXXXXXA\r\n'
reply: retcode (250); Msg: mx10.comcast.com says EHLO to 129.6.220.67:57015
SIZE 40000000
PIPELINING
ENHANCEDSTATUSCODES
8BITMIME
XXXXXXXA
send: 'mail FROM:<no-such-sender@comcast.com> size=137\r\n'
reply: '250 2.0.0 MAIL FROM accepted\r\n'
reply: retcode (250); Msg: 2.0.0 MAIL FROM accepted
send: 'rcpt TO:<no-such-receiver@comcast.com>\r\n'
reply: '550 5.1.1 Recipient address rejected: {Gateway}\r\n'
reply: retcode (550); Msg: 5.1.1 Recipient address rejected: {Gateway}
send: 'rset\r\n'
reply: '250 2.0.0 RSET OK\r\n'
reply: retcode (250); Msg: 2.0.0 RSET OK
Traceback (most recent call last):
File "/Users/simsong/x.py", line 11, in <module>
""")
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 742, in sendmail
raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'no-such-receiver@comcast.com': (550, '5.1.1 Recipient address rejected: {Gateway}')}
send:'ehlo dance.local\r\n'
答复:“250-mx10.comcast.com说EHLO发送到129.6.220.67:57015\r\n”
答复:“250-SIZE 40000000\r\n”
答复:“250-流水线\r\n”
答复:“250-ENHANCEDSTATUSCODES\r\n”
答复:“250-8BITMIME\r\n”
答复:“250 XXXXXX A\r\n”
答复:retcode(250);Msg:mx10.comcast.com说EHLO到129.6.220.67:57015
大小40000000
流水线
增强状态码
8比特MIME
XXXXXXXX A
发送:“邮件发件人:大小=137\r\n”
答复:“250 2.0.0来自已接受邮件的邮件\r\n”
答复:retcode(250);Msg:2.0.0已接受来自的邮件
发送:'rcpt至:\r\n'
答复:“550 5.1.1收件人地址被拒绝:{Gateway}\r\n”
答复:retcode(550);;Msg:5.1.1拒绝的收件人地址:{Gateway}
发送:“rset\r\n”
答复:“250 2.0.0 RSET正常\r\n”
答复:retcode(250);Msg:2.0.0 RSET正常
回溯(最近一次呼叫最后一次):
文件“/Users/simsong/x.py”,第11行,在
""")
sendmail中的文件“/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py”,第742行
升起SMTPRecipientsRefused(发送器)
smtplib.smtprecipientsreference:{'no-soke-receiver@comcast.com':(550,'5.1.1收件人地址被拒绝:{Gateway}')}
我希望输出是一个变量,output
。具体来说,我希望所有以send:
和reply:
开头的行研究错误:
a = None
try:
s.sendmail("no-such-sender@comcast.com" ["no-suchreceiver@comcast.com"],"""
from: no-such-sender@comcast.com
to: no-such-receiver@comcast.com
subject: no such message
This message won't be delivered to anybody.
""")
except smtplib.SMTPRecipientsRefused as e:
a = e
现在,您可以了解如何提取它:
a.args
message = None
try:
s.sendmail("...")
except smtplib.SMTPException as e:
message = e.args[0]['no-such-receiver@comcast.com'][1]
({不是这样的-receiver@comcast.com': (550,
b'5.1.1拒绝的收件人地址:{Gateway}')},)
b'5.1.1拒绝的收件人地址:{Gateway}'
这是你的留言
因此,要提取它:
a.args
message = None
try:
s.sendmail("...")
except smtplib.SMTPException as e:
message = e.args[0]['no-such-receiver@comcast.com'][1]
可以通过将stderr重定向到一个文件来完成:
import tempfile, smtplib, os, sys
# Find an available file descriptor
t = tempfile.TemporaryFile()
available_fd = t.fileno()
t.close()
# now make a copy of stderr
os.dup2(2,available_fd)
# Now create a new tempfile and make Python's stderr go to that file
t = tempfile.TemporaryFile()
os.dup2(t.fileno(),2)
# Now run the task that logs to stderr
s = smtplib.SMTP("mx10.comcast.com")
s.set_debuglevel(1)
s.sendmail("no-such-sender@comcast.com",["no-such-receiver@comcast.com"],"""
from: no-such-sender@comcast.com
to: no-such-receiver@comcast.com
subject: no such message
This message won't be delivered to anybody.
""")
# Grab the stderr from the temp file
sys.stderr.flush()
t.flush()
t.seek(0)
stderr_output = t.read()
t.close()
# Put back stderr
os.dup2(available_fd,2)
os.close(available_fd)
# Finally, demonstrate that we have the output:
print("STDERR:")
count = 0
for line in stderr_output.decode('utf-8').split("\n"):
count += 1
print("{:3} {}".format(count,line))
谢谢,但我不想要Python异常,我想要SMTP成绩单。所有以
send:
和reply: