{"id":234,"date":"2018-11-18T12:46:44","date_gmt":"2018-11-18T12:46:44","guid":{"rendered":"http:\/\/web.recologypower.com:9080\/?p=234"},"modified":"2019-09-08T14:30:31","modified_gmt":"2019-09-08T14:30:31","slug":"python","status":"publish","type":"post","link":"http:\/\/www.recologypower.com:9080\/?p=234","title":{"rendered":"python3 \u4e2dssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)\u7684\u9519\u8bef"},"content":{"rendered":"<pre class=\"brush:other\">File \"\/Library\/Frameworks\/Python.framework\/Versions\/3.6\/lib\/python3.6\/urllib\/request.py\", line 1318, in do_open\r\n\u00a0 \u00a0 encode_chunked=req.has_header('Transfer-encoding'))\r\n\u00a0 File \"\/Library\/Frameworks\/Python.framework\/Versions\/3.6\/lib\/python3.6\/http\/client.py\", line 1239, in request\r\n\u00a0 \u00a0 self._send_request(method, url, body, headers, encode_chunked)\r\n\u00a0 File \"\/Library\/Frameworks\/Python.framework\/Versions\/3.6\/lib\/python3.6\/http\/client.py\", line 1285, in _send_request\r\n\u00a0 \u00a0 self.endheaders(body, encode_chunked=encode_chunked)\r\n\u00a0 File \"\/Library\/Frameworks\/Python.framework\/Versions\/3.6\/lib\/python3.6\/http\/client.py\", line 1234, in endheaders\r\n\u00a0 \u00a0 self._send_output(message_body, encode_chunked=encode_chunked)\r\n\u00a0 File \"\/Library\/Frameworks\/Python.framework\/Versions\/3.6\/lib\/python3.6\/http\/client.py\", line 1026, in _send_output\r\n\u00a0 \u00a0 self.send(msg)\r\n\u00a0 File \"\/Library\/Frameworks\/Python.framework\/Versions\/3.6\/lib\/python3.6\/http\/client.py\", line 964, in send\r\n\u00a0 \u00a0 self.connect()\r\n\u00a0 File \"\/Library\/Frameworks\/Python.framework\/Versions\/3.6\/lib\/python3.6\/http\/client.py\", line 1400, in connect\r\n\u00a0 \u00a0 server_hostname=server_hostname)\r\n\u00a0 File \"\/Library\/Frameworks\/Python.framework\/Versions\/3.6\/lib\/python3.6\/ssl.py\", line 407, in wrap_socket\r\n\u00a0 \u00a0 _context=self, _session=session)\r\n\u00a0 File \"\/Library\/Frameworks\/Python.framework\/Versions\/3.6\/lib\/python3.6\/ssl.py\", line 814, in __init__\r\n\u00a0 \u00a0 self.do_handshake()\r\n\u00a0 File \"\/Library\/Frameworks\/Python.framework\/Versions\/3.6\/lib\/python3.6\/ssl.py\", line 1068, in do_handshake\r\n\u00a0 \u00a0 self._sslobj.do_handshake()\r\n\u00a0 File \"\/Library\/Frameworks\/Python.framework\/Versions\/3.6\/lib\/python3.6\/ssl.py\", line 689, in do_handshake\r\n\u00a0 \u00a0 self._sslobj.do_handshake()\r\nssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)<\/pre>\n<p>&nbsp;<\/p>\n<p>python2.7\u5347\u7ea7\u5230python3\u9700\u8981\u5bf9ssl\u8fdb\u884c\u6821\u9a8c<\/p>\n<p>\u6240\u4ee5\u9700\u8981\u5f15\u5165\u6a21\u5757ssl<\/p>\n<p>import ssl<\/p>\n<p>\u5728urlopen\u4e2d<\/p>\n<p>context\u6dfb\u52a0ssl._create_unverified_context()<\/p>\n<p>\u5173\u95edssl\u6821\u9a8c\u5373\u53ef<\/p>\n<pre class=\"brush:other\">url = \"https:\/\/www.baidu.com\"context = ssl._create_unverified_context()get = urllib.request.urlopen(url, context = context ).read()\r\n\r\nprint(get)<\/pre>\n<p>&nbsp;<\/p>\n<p>\u4f46\u662f\u8fd9\u79cd\u529e\u6cd5\u4e0d\u63a8\u8350\uff0c\u56e0\u4e3a\u6700\u597d\u4e0d\u8981\u5173\u95ed\u9a8c\u8bc1<\/p>\n<pre class=\"brush:other\">&gt; from urllib import request\r\n&gt;&gt;&gt; url = 'https:\/\/www.baidu.com'\r\n&gt;&gt;&gt; with request.urlopen(url) as f:\r\n... data = f.read()\r\n... print(f.status)<\/pre>\n<p>&nbsp;<\/p>\n<p>\u4ee5\u4e0a\u529e\u6cd5\u53ef\u80fd\u4f1a\u65e0\u6548<\/p>\n<p>\u53d6\u6d88\u5168\u5c40\u9a8c\u8bc1<\/p>\n<pre class=\"brush:other\">import ssl\r\n\r\nssl._create_default_https_context = ssl._create_unverified_context<\/pre>\n<p>&nbsp;<\/p>\n<p>\u7136\u540e\u4f7f\u7528urllib.urlopen(&#8216;url&#8217;)<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"slide-text-bg2\">\n<h3>File &quot;\/Library\/Frame<\/h3>\n<\/div>\n<div class=\"flex-btn-div\"><a href=\"http:\/\/www.recologypower.com:9080\/?p=234\" class=\"btn1 flex-btn\">\u9605\u8bfb\u66f4\u591a<\/a><\/div>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,13],"tags":[],"_links":{"self":[{"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/posts\/234"}],"collection":[{"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=234"}],"version-history":[{"count":1,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/posts\/234\/revisions"}],"predecessor-version":[{"id":235,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/posts\/234\/revisions\/235"}],"wp:attachment":[{"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}