python winrm协议抛出警告:如何防止?

2024-09-19 22:04:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个代码,它工作得很好,但它打印警告,我想阻止他们显示 因为它会在日志中造成混乱

 try:
        p = Protocol(
            endpoint='http://{}:{}/wsman'.format("10.x.x.4x", "5xxxx"),
            transport='ntlm',
            username='xxxx',
            password='xxxx',
            server_cert_validation='ignore')

        shell_id = p.open_shell()
        command_id = p.run_command(shell_id, command, command_args)
        std_out, std_err, status_code = p.get_command_output(shell_id, command_id)
        p.cleanup_command(shell_id, command_id)
        p.close_shell(shell_id)

        if status_code == 1:
            logger.error("execute_win_command failed: %s %s %s" % (std_out, std_err, status_code))
            return False

        logger.info(
            "execute_win_command success: %s %s %s" % (std_out, connection_map['ip'], connection_map['port']))

    except Exception as err:
        if std_err is not None and status_code is not None and std_out is not None:
            logger.error("execute_win_command  %s %s %s" % (std_out, std_err, status_code))
        logger.error("At %s exception: %s - %s :%s" %
                     ("execute_win_command", current_user.username, repr(err), traceback.format_exc()))
        return False

我明白了:

WARNING:urllib3.connectionpool:Failed to parse headers (url=http://10.x.x.4x:5xxxx/wsman): [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
Traceback (most recent call last):
  File "C:\Python36-64\lib\site-packages\urllib3\connectionpool.py", line 446, in _make_request
    assert_header_parsing(httplib_response.msg)
  File "C:\Python36-64\lib\site-packages\urllib3\util\response.py", line 71, in assert_header_parsing
    raise HeaderParsingError(defects=defects, unparsed_data=unparsed_data)
urllib3.exceptions.HeaderParsingError: [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
WARNING:urllib3.connectionpool:Failed to parse headers (url=http://10.x.x.4x:5xxxx/wsman): [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
Traceback (most recent call last):
  File "C:\Python36-64\lib\site-packages\urllib3\connectionpool.py", line 446, in _make_request
    assert_header_parsing(httplib_response.msg)
  File "C:\Python36-64\lib\site-packages\urllib3\util\response.py", line 71, in assert_header_parsing
    raise HeaderParsingError(defects=defects, unparsed_data=unparsed_data)
urllib3.exceptions.HeaderParsingError: [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
WARNING:urllib3.connectionpool:Failed to parse headers (url=http://10.x.x.4x:5xxxx/wsman): [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
Traceback (most recent call last):
  File "C:\Python36-64\lib\site-packages\urllib3\connectionpool.py", line 446, in _make_request
    assert_header_parsing(httplib_response.msg)
  File "C:\Python36-64\lib\site-packages\urllib3\util\response.py", line 71, in assert_header_parsing
    raise HeaderParsingError(defects=defects, unparsed_data=unparsed_data)
urllib3.exceptions.HeaderParsingError: [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
WARNING:urllib3.connectionpool:Failed to parse headers (url=http://10.x.x.4x:5xxxx/wsman): [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
Traceback (most recent call last):
  File "C:\Python36-64\lib\site-packages\urllib3\connectionpool.py", line 446, in _make_request
    assert_header_parsing(httplib_response.msg)
  File "C:\Python36-64\lib\site-packages\urllib3\util\response.py", line 71, in assert_header_parsing
    raise HeaderParsingError(defects=defects, unparsed_data=unparsed_data)
urllib3.exceptions.HeaderParsingError: [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
WARNING:urllib3.connectionpool:Failed to parse headers (url=http://10.x.x.4x:5xxxx/wsman): [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
Traceback (most recent call last):
  File "C:\Python36-64\lib\site-packages\urllib3\connectionpool.py", line 446, in _make_request
    assert_header_parsing(httplib_response.msg)
  File "C:\Python36-64\lib\site-packages\urllib3\util\response.py", line 71, in assert_header_parsing
    raise HeaderParsingError(defects=defects, unparsed_data=unparsed_data)
urllib3.exceptions.HeaderParsingError: [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''

Tags: inpydatalibpackageslinesitecommand
2条回答


使用标准python库“日志记录”抑制日志:


将此代码放在现有代码的顶部,以忽略urllib3

import logging
urllib3_logger = logging.getLogger('urllib3')
urllib3_logger.setLevel(logging.CRITICAL)

可以在脚本启动时添加此行

urllib3.disable_warnings()

您还可以通过环境变量PYTHONWARNINGS执行此操作:

export PYTHONWARNINGS="ignore:Failed to parse headers"

相关问题 更多 >