处理获取socket.gaierror的错误:[Errno 11001] getaddrinfo在python中使用pyopenssl失败

时间:2019-12-27 20:55:12

标签: python ssl exception ssl-certificate pyopenssl

摘要: 我会在第一遍中尝试解释一下,但是如果我错过了一些相关的细节,请告诉我,我将继续进行下去。我正在从文本文件中提取服务器列表,并使用pyopenssl来解析,连接和检索SSL证书信息。根据到期日期,我正在计算证书到期之前的天数。我的结果通过数组传递,格式化,然后通过电子邮件发送。 直到我的其中一台服务器无法解析并且得到socket.gaierror,它才能正常工作。

问题:当主机无法解决问题时,它会炸毁我的结果,并且出现以下错误。尽管它正确记录了错误,但是我尝试传递一些能够以主机,主机名,到期日期和到期天的格式记录我的结果中的异常的东西,这样我就可以传递给表并发送电子邮件。我希望它在主机字段中注明“无法解决”,并且可能导致到期日期和到期天数仅为“ 0”。谁能指出我实现这一目标的好方法?谢谢!

错误消息:

app/launcher.js

Python代码

Checking certificate for server  server.domain.com
--- Logging error ---
Traceback (most recent call last):
  File "ssl_nag_script_test.py", line 80, in <module>
    s.connect((host, int(port)))
socket.gaierror: [Errno 11001] getaddrinfo failed.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "E:\Python36\lib\logging\__init__.py", line 992, in emit
    msg = self.format(record)
  File "E:\Python36\lib\logging\__init__.py", line 838, in format
    return fmt.format(record)
  File "E:\Python36\lib\logging\__init__.py", line 575, in format
    record.message = record.getMessage()
  File "E:\Python36\lib\logging\__init__.py", line 338, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting
Call stack:
  File "ssl_nag_script_test.py", line 106, in <module>
    logger.warning('Error on connection to Server,', str(ip))
Message: 'Error on connection to Server,'
Arguments: ('server.domain.com:443\n',)
--- Logging error ---
Traceback (most recent call last):
  File "ssl_nag_script_test.py", line 80, in <module>
    s.connect((host, int(port)))
socket.gaierror: [Errno 11001] getaddrinfo failed.

**Then**
WARNING:root:ERROR ENCOUNTERED!
WARNING:root:Traceback (most recent call last):
  File "ssl_nag_script - Copy.py", line 114, in <module>
    key=lambda k: k[1]['days_to_expire'], reverse=False)
TypeError: '<' not supported between instances of 'str' and 'int'

1 个答案:

答案 0 :(得分:0)

你有

logger.warning('Error on connection to Server,', str(ip))

但是应该是:

logger.warning('Error on connection to Server "%s"', str(ip))

或等价物。

您正在传递一个额外的参数str(ip),但是消息中没有放置它的占位符,因此,错误的以下部分:

  File "E:\Python36\lib\logging\__init__.py", line 338, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting
Call stack:
  File "ssl_nag_script_test.py", line 106, in <module>
    logger.warning('Error on connection to Server,', str(ip))
相关问题