在 mechanize 中禁用 ssl 证书验证

Disable ssl certificate validation in mechanize

我是 python 的新手,我正在尝试使用 mechanize 访问网站。

br = mechanize.Browser()
r=br.open("https://172.22.2.2/")

这给了我以下错误:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    br.open("https://172.22.2.2/")
  File "/home/freeza/.local/lib/python2.7/site-packages/mechanize/_mechanize.py", line 203, in open
    return self._mech_open(url, data, timeout=timeout)
  File "/home/freeza/.local/lib/python2.7/site-packages/mechanize/_mechanize.py", line 230, in _mech_open
    response = UserAgentBase.open(self, request, data)
  File "/home/freeza/.local/lib/python2.7/site-packages/mechanize/_opener.py", line 193, in open
    response = urlopen(self, req, data)
  File "/home/freeza/.local/lib/python2.7/site-packages/mechanize/_urllib2_fork.py", line 344, in _open
'_open', req)
  File "/home/freeza/.local/lib/python2.7/site-packages/mechanize/_urllib2_fork.py", line 332, in _call_chain
    result = func(*args)
  File "/home/freeza/.local/lib/python2.7/site-packages/mechanize/_urllib2_fork.py", line 1170, in https_open
    return self.do_open(conn_factory, req)
  File "/home/freeza/.local/lib/python2.7/site-packages/mechanize/_urllib2_fork.py", line 1118, in do_open
    raise URLError(err)
URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>

你能告诉我如何在 python 中禁用 mechanize 中的 ssl 证书验证吗?

如果我得到证书,你能告诉我如何包含证书吗? 谢谢

添加此代码段以在 br.open() 之前禁用 HTTPS 证书验证。

import ssl
try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    # Legacy Python that doesn't verify HTTPS certificates by default
    pass
else:
    # Handle target environment that doesn't support HTTPS verification
    ssl._create_default_https_context = _create_unverified_https_context

对于要求解释的人:检查此 link