【问题标题】:Suppress output selectively python [duplicate]选择性地抑制输出python [重复]
【发布时间】:2015-12-23 08:57:52
【问题描述】:

我如何能够有选择地输出某些 python 函数而不输出其他函数?

代码:

#!/usr/bin/env python

import os
import sys
import pprint

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

from m2x.client import M2XClient


KEY = '10e34b827d5e1cd90fds9d011006a7b4'
DEVICE_ID = '161add732eb725402d1182dafd74f907'

client = M2XClient(key=KEY)
device = client.device(DEVICE_ID)

cont = raw_input('Continue? [y/n] ')

if cont == 'y':
    print 'Continued...'
else:
    sys.exit()

输出:

/usr/local/lib/python2.7/dist-packages/requests-2.9.1-    py2.7.egg/requests/packages/urllib3/util/ssl_.py:315: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/usr/local/lib/python2.7/dist-packages/requests-2.9.1-py2.7.egg/requests/packages/urllib3/util/ssl_.py:120: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
Continue? [y/n]

用户想要的输出:

Continue? [y/n]

【问题讨论】:

  • @tmoreau 这是一个错误的副本。 OP 只想禁用某些特定的模块警告,而不是全部。

标签: python output


【解决方案1】:

如果生成警告的是 urllib3 库,那么最好查看它的文档:

http://urllib3.readthedocs.org/en/latest/security.html#disabling-warnings

所以:

import urllib3
urllib3.disable_warnings()

也就是说,抑制有关 HTTPS 安全性的警告可能是一个非常糟糕的主意。 :-)

【讨论】:

  • urllib3.disable_warnings() AttributeError: 'module' 对象没有属性 'disable_warnings'
  • 需要ssl支持:pip install pyopenssl ndg-httpsclient pyasn1
  • 按照@lbsweek 的指示,警告消失了。无需禁用它们。
  • 节省了我的几个小时......谢谢
猜你喜欢
  • 2016-08-17
  • 2011-11-21
  • 1970-01-01
  • 2021-11-06
  • 2016-08-23
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 2023-03-18
相关资源
最近更新 更多