||
異常狀態發生
# 載入模組 import urllib.request as request # 指定網址 src = "https://python.org/" # 開啟網址資料 request.urlopen(src)
執行異常訊息: Traceback (most recent call last): File "...PythonPython39liburllib equest.py", line 1346, in do_open h.request(req.get_method(), req.selector, req.data, headers, File "...PythonPython39libhttpclient.py", line 1253, in request self._send_request(method, url, body, headers, encode_chunked) File "...PythonPython39libhttpclient.py", line 1299, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "...PythonPython39libhttpclient.py", line 1248, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "...PythonPython39libhttpclient.py", line 1008, in _send_output self.send(msg) File "...PythonPython39libhttpclient.py", line 948, in send self.connect() File "...PythonPython39libhttpclient.py", line 1422, in connect self.sock = self._context.wrap_socket(self.sock, File "...PythonPython39libssl.py", line 500, in wrap_socket return self.sslsocket_class._create( File "...PythonPython39libssl.py", line 1040, in _create self.do_handshake() File "...PythonPython39libssl.py", line 1309, in do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129)
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "... et_data.py", line 9, in <module> request.urlopen(src) File "...PythonPython39liburllib equest.py", line 214, in urlopen return opener.open(url, data, timeout) File "...PythonPython39liburllib equest.py", line 517, in open response = self._open(req, data) File "...PythonPython39liburllib equest.py", line 534, in _open result = self._call_chain(self.handle_open, protocol, protocol + File "...PythonPython39liburllib equest.py", line 494, in _call_chain result = func(*args) File "...PythonPython39liburllib equest.py", line 1389, in https_open return self.do_open(http.client.HTTPSConnection, req, File "...PythonPython39liburllib equest.py", line 1349, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129)> |
搜尋異常狀態
# 載入模組 import urllib.request as request # 指定網址 src = "https://python.org/" # 開啟網址資料 #request.urlopen(src) from urllib.error import HTTPError, URLError try: request.urlopen(src) print("urlopen OK") except HTTPError as e: print("HTTP code error: ", e.reason) except URLError as error: print("URLError code error: ", error.reason) 顯示執行異常訊息: URLError code error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129) 問題發生原因: 驗證 SSL 憑證異常。 |
修改說明:選擇不用認證此 SSL 憑證
# 載入模組 import urllib.request as request import ssl from urllib.error import HTTPError, URLError # 指定網址 src = "https://python.org/" # 取消 SSL 憑證 認證 ssl._create_default_https_context = ssl._create_unverified_context # 開啟網址資料 try: request.urlopen(src) print("urlopen OK") except HTTPError as e: print("HTTP code error: ", e.reason) except URLError as error: print("URLError code error: ", error.reason)
顯示正確執行訊息: urlopen OK |