从Python smtplib捕获调试输出 [英] Capture debug output from Python smtplib

查看:263
本文介绍了从Python smtplib捕获调试输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何捕获Python smtplib库中的调试输出?

How do I capture the debug output from the Python smtplib library?

这是我的测试程序:

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}')}

我希望输出变量为 output 。所有以 send: reply:开头的行。

I want the output in a variable, output. Specifically, I want all of the lines that begin with send: and reply:.

推荐答案

可以通过将stderr重定向到文件来完成:

It can be done by redirecting stderr to a file:

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 smtplib捕获调试输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆