'Python SSLCertVerificationError When running a unittest
I am currently working on a final exam for a python programming course and can't seem to get past the error below (SSLCertVerificationError) during the unit test. I am attempting to use pull text from a url by a tag and id. I've tried many solutions like running the "install certificates.command" and some answers said it could be a server issue. My instructor said that we cannot use them as a resource so I wanted to exhaust all options before reaching out to them.
Code -
def __init__(self, src, _src_type = 'discover', _content = 'none', _orig_content = 'none'):
self.src = src
self._srctype = _src_type
self._content = None
self._orig_content = None
if re.search('^http', src):
self._src_type = 'url'
elif re.search('txt$', src):
self._src_type = 'path'
else:
self._src_type = 'text'
def set_content_to_tag(self, tag, tag_id=None):
self.tag = tag
self.tag_id = tag_id
if self._src_type == 'url':
r = requests.get(self.src)
self._orig_content = r.text
else:
raise Exception('You are attempting to search an instance that is not a url')
soup = BeautifulSoup(self._orig_content, 'html.parser')
self._content = soup.findall(self.tag, self.tag_id).get_text()
Unittest -
import unittest
url = 'https://www.webucator.com/how-to/address-by-bill-clinton-1997.cfm'
path = 'pride-and-prejudice.txt'
text = '''The outlook wasn't brilliant for the Mudville Nine that day;
the score stood four to two, with but one inning more to play.
And then when Cooney died at first, and Barrows did the same,
a sickly silence fell upon the patrons of the game.'''
class TestTextAnalyzer(unittest.TestCase):
def test_discover_url(self):
ta = TextAnalyzer(url)
self.assertEqual(ta._src_type, 'url')
def test_discover_path(self):
ta = TextAnalyzer(path)
self.assertEqual(ta._src_type, 'path')
def test_discover_text(self):
ta = TextAnalyzer(text)
self.assertEqual(ta._src_type, 'text')
def test_set_content_to_tag(self):
ta = TextAnalyzer(url)
ta.set_content_to_tag('div','content-main')
self.assertEqual(ta._content[0:23], 'Address by Bill Clinton')
Error Message -
Traceback (most recent call last):
File "/Users/user/Library/Python/3.9/lib/python/site-packages/urllib3/connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "/Users/user/Library/Python/3.9/lib/python/site-packages/urllib3/connectionpool.py", line 386, in _make_request
self._validate_conn(conn)
File "/Users/user/Library/Python/3.9/lib/python/site-packages/urllib3/connectionpool.py", line 1040, in _validate_conn
conn.connect()
File "/Users/user/Library/Python/3.9/lib/python/site-packages/urllib3/connection.py", line 416, in connect
self.sock = ssl_wrap_socket(
File "/Users/user/Library/Python/3.9/lib/python/site-packages/urllib3/util/ssl_.py", line 449, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(
File "/Users/user/Library/Python/3.9/lib/python/site-packages/urllib3/util/ssl_.py", line 493, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1040, in _create
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/user/Library/Python/3.9/lib/python/site-packages/requests/adapters.py", line 440, in send
resp = conn.urlopen(
File "/Users/user/Library/Python/3.9/lib/python/site-packages/urllib3/connectionpool.py", line 785, in urlopen
retries = retries.increment(
File "/Users/user/Library/Python/3.9/lib/python/site-packages/urllib3/util/retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.webucator.com', port=443): Max retries exceeded with url: /how-to/address-by-bill-clinton-1997.cfm (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/folders/pz/mf7h9hjd63q940q4kr31l7c00000gp/T/ipykernel_6591/2348624285.py", line 22, in test_set_content_to_tag
ta.set_content_to_tag('div','content-main')
File "/var/folders/pz/mf7h9hjd63q940q4kr31l7c00000gp/T/ipykernel_6591/2563368935.py", line 32, in set_content_to_tag
r = requests.get(self.src)
File "/Users/user/Library/Python/3.9/lib/python/site-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/Users/user/Library/Python/3.9/lib/python/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/Users/user/Library/Python/3.9/lib/python/site-packages/requests/sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "/Users/user/Library/Python/3.9/lib/python/site-packages/requests/sessions.py", line 645, in send
r = adapter.send(request, **kwargs)
File "/Users/user/Library/Python/3.9/lib/python/site-packages/requests/adapters.py", line 517, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='www.webucator.com', port=443): Max retries exceeded with url: /how-to/address-by-bill-clinton-1997.cfm (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)')))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
