在机械化中禁用SSL证书验证

在机械化中禁用SSL证书验证

本文介绍了在机械化中禁用SSL证书验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,我正尝试使用机械化访问网站.

I am new to python and I was trying to access a website using 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机械化中禁用ssl证书验证吗?

Can you tell me how to disable ssl certificate validation in mechanize in python?

如果我获得了证书,您还能告诉我如何包括它吗?谢谢

Also can you tell me how to include certificate if I get it?Thanks

推荐答案

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

Add this code snippet to disable HTTPS certificate validation before br.open().

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

对于要求解释的人们:请检查此链接.

For people asking for explanation: check this link.

这篇关于在机械化中禁用SSL证书验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 07:42