【发布时间】:2022-11-20 09:21:50
【问题描述】:
我正在尝试在我的 python 应用程序中设置严重错误的电子邮件日志记录。我在尝试初始化 SMTPHandler 时一直遇到错误:
AttributeError: 'SMTPHandler' 对象没有属性 'credentials'
我正在使用 Python 3.10。我在出现错误的地方创建了一个程序组件。
import logging
from logging.handlers import SMTPHandler
mail_handler = SMTPHandler(
mailhost='my.hosting.com',
fromaddr='admin@myapp.com',
toaddrs=['admin@myapp.com'],
subject='Application Error',
credentials=('admin@myapp.com', 'mypassword'),
secure=()
)
print(mail_handler.mailhost)
print(mail_handler.fromaddr)
print(mail_handler.toaddrs)
print(mail_handler.subject)
print(mail_handler.secure)
print(mail_handler.timeout)
print(mail_handler.credentials)
mail_handler.setLevel(logging.ERROR)
mail_handler.setFormatter(logging.Formatter('[%(asctime)s] %(levelname)s in %(module)s: %(message)s'))
我得到的打印语句和回溯是:
my.hosting.com
admin@myapp.com
['admin@myapp.com']
Application Error
()
5.0
Traceback (most recent call last):
File "C:\Users\user\Documents\myapp\test.py", line 31, in <module>
print(mail_handler.credentials)
AttributeError: 'SMTPHandler' object has no attribute 'credentials'
当我使用以下 sn-p 检查 SMTPHandler 的 init 语句以确保我没有访问非常旧的版本时(我认为凭据是在 2.6 中添加的):
import inspect
signature = inspect.signature(SMTPHandler.__init__).parameters
for name, parameter in signature.items():
print(name, parameter.default, parameter.annotation, parameter.kind)`
我得到:
self <class 'inspect._empty'> <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
mailhost <class 'inspect._empty'> <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
fromaddr <class 'inspect._empty'> <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
toaddrs <class 'inspect._empty'> <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
subject <class 'inspect._empty'> <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
credentials None <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
secure None <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
timeout 5.0 <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
所以'credentials'在初始化语句中。
有人在我的代码中看到了一些愚蠢的东西或遇到了这个问题吗?
非常感谢!
【问题讨论】:
标签: python credentials