编译的Python脚本"没有为logger找到处理程序" paramiko.transport"

时间:2016-10-27 19:49:39

标签: python python-2.7 ssh handler

我的问题与此问题非常相似: No handlers could be found for logger paramiko

不同之处在于我的脚本在Python解释器中运行得非常好,但在使用PyInstaller编译到exe文件时会抛出上述错误。我尝试了几个不同的日志处理程序,没有运气,而且我正在为可以从任何Windows系统运行的自包含可执行文件进行拍摄。这是我的脚本供参考:

from netmiko import ConnectHandler
net_connect = ConnectHandler(device_type='hp_procurve', ip='10.1.2.20', username='myusername', password='mypassword')  
output = net_connect.send_command("show run")
print output
net_connect.disconnect()
raw_input("Press enter to exit...")

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

将一个控制台处理程序(例如)添加到paramiko.transport:

paramiko_logger = logging.getLogger('paramiko.transport')
if not paramiko_logger.handlers:
    console_handler = logging.StreamHandler()
    console_handler.setFormatter(
        logging.Formatter('%(asctime)s | %(levelname)-8s| PARAMIKO: '
                          '%(lineno)03d@%(module)-10s| %(message)s')
        )
    paramiko_logger.addHandler(console_handler)

答案 1 :(得分:0)

对于任何遇到此问题的人来说,这是我的完整Python脚本,可以通过SSH连接到HP ProCurve交换机并打印“show run”命令的输出。我遇到了另一个错误,在这篇帖子中提到的错误解决后,加密模块无法与PyInstaller正常交互。此链接提供了该解决方案: https://github.com/pyinstaller/pyinstaller/issues/2013

def patch_crypto_be_discovery():
    from cryptography.hazmat import backends
    try:
        from cryptography.hazmat.backends.commoncrypto.backend import backend as be_cc
    except ImportError:
        be_cc = None
    try:
        from cryptography.hazmat.backends.openssl.backend import backend as be_ossl
    except ImportError:
        be_ossl = None
    backends._available_backends_list = [
        be for be in (be_cc, be_ossl) if be is not None
]
import logging
paramiko_logger = logging.getLogger('paramiko.transport')
if not paramiko_logger.handlers:
    console_handler = logging.StreamHandler()
    console_handler.setFormatter(
        logging.Formatter('%(asctime)s | %(levelname)-8s| PARAMIKO: '
                      '%(lineno)03d@%(module)-10s| %(message)s')
    )
paramiko_logger.addHandler(console_handler)
from netmiko import ConnectHandler
net_connect = ConnectHandler(device_type='hp_procurve', ip='10.1.2.20', username='myusername', password='mypassword')  
output = net_connect.send_command("show run")
print output
net_connect.disconnect()
raw_input("Press enter to exit...")

我还需要运行此命令将脚本编译为exe:

pyinstaller --onefile --hidden-import cryptography.hazmat.backends.openssl --hidden-import cffi HP-ProCurve-ShowRun.py