使用Paramiko Python模块时如何避免这些keepalive@openssh.com日志消息?

时间:2017-12-20 06:01:05

标签: python python-2.7 logging sftp paramiko

我正在使用Python Paramiko模块从SFTP服务器下载文件。

但是它会连续显示以下日志消息。

Wed Dec 20 06:51:00 2017 transport         line:1572 22192 /PoolWorker-13        139861396514560 Thread-138           DEBUG    Received global request "keepalive@openssh.com"
Wed Dec 20 06:51:00 2017 transport         line:1572 22192 /PoolWorker-13        139861396514560 Thread-138           DEBUG    Rejecting "keepalive@openssh.com" global request from server.
Wed Dec 20 06:51:00 2017 transport         line:1572 22192 /PoolWorker-13        139861395986176 Thread-140           DEBUG    Received global request "keepalive@openssh.com"
Wed Dec 20 06:51:00 2017 transport         line:1572 22192 /PoolWorker-13        139861395986176 Thread-140           DEBUG    Rejecting "keepalive@openssh.com" global request from server.

避免在Paramiko模块的日志中显示上述行所需的更改是什么?我在Python 2.7.6上使用Paramiko 2.1.2模块。如果我们使用pysftp模块而不是Paramiko,这些日志行是否会消失?

1 个答案:

答案 0 :(得分:2)

使用Logger.addFilter过滤掉它们:

class KeepalivesFilter (object):
    def filter(self, record):
        return record.msg.find('keepalive@openssh.com') < 0

paramiko.util.get_logger('paramiko.transport').addFilter(KeepalivesFilter())

(使用pysftp无助于内部使用Paramiko)