我目前正在尝试使用 Python 日志模块中的 SMTPHandler 类进行日志记录。记录器实例化得很好,但在调用 _socket.getaddrinfo 时失败,尽管它正在被传递
我目前正在尝试使用 Python 日志模块中的 SMTPHandler 类进行日志记录。记录器实例化得很好,但在调用时失败, _socket.getaddrinfo
尽管它传递了似乎在其他地方有效的参数。以下是我收到的错误消息:
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\logging\handlers.py", line 1075, in emit
smtp = smtplib.SMTP(self.mailhost, port, timeout=self.timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\socket.py", line 827, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\socket.py", line 962, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 11001] getaddrinfo failed
Call stack:
File "<string>", line 1, in <module>
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\idlelib\run.py", line 165, in main
ret = method(*args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\idlelib\run.py", line 579, in runcode
exec(code, self.locals)
File "C:\Users\User\Desktop\test_log\test_log.py", line 34, in <module>
main()
File "C:\Users\User\Desktop\test_log\test_log.py", line 28, in main
log.critical('Testing email logging')
Message: 'Testing email logging'
Arguments: ()
我正在配置电子邮件处理程序并尝试按如下所示进行记录。
处理程序配置:
"emailhand": {
"class": "logging.handlers.SMTPHandler",
"level": "CRITICAL",
"formatter": "a_formatter",
"mailhost": "('send.smtp.com', 25)",
"credentials": "('[email protected]', 'password')",
"fromaddr": "[email protected]",
"toaddrs": "[email protected]",
"subject": "Test Log",
"secure": "()",
"timeout": 10.0
}
Python 模块:
import os
import json
import logging
import logging.config
import logging.handlers
def init_logging():
logfile = r'C:\Users\User\Desktop\test_log\test_log.log'
with open(r'C:\Users\User\Desktop\test_log\file-data\logging.json', 'r') as f:
LOGGING_CONFIG = json.load(f)
logging.config.dictConfig(LOGGING_CONFIG)
def main():
init_logging()
log = logging.getLogger('test_log')
log.info('Some info from the main function')
log.critical('Testing email logging')
if __name__ == "__main__":
main()
目前,我在连接到电子邮件服务器时使用的是 TLS,但我也尝试过使用 SSL 和端口 465,但这也没有用。我能够通过使用 smtplib.SMTP 成功使用此模块之外的 smtp 库发送电子邮件,同时使用 TLS 和 SSL。我尝试更改超时值(从 1 到 10000 的任意值),但这似乎没有什么区别。我还尝试更改 emit 函数以使用 SMTP_SSL 而不是 SMTP,但这没有用。我使用与上述相同的主机和端口运行了 socket.getaddrinfo,并收到以下内容(我已将 IP 数字替换为“#”):
[(<AddressFamily.AF_INET: 2>, 0, 0, '', ('###.##.###.##', 25)), (<AddressFamily.AF_INET: 2>, 0, 0, '', ('###.##.###.##', 25))]
据我所知,上述情况意味着套接字工作正常。什么原因导致了这个问题?