๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT-Engineering/TroubleShooting

urllib request.py urlopen SSL ์ธ์ฆ์„œ ์—๋Ÿฌ ๋ฌธ์ œ ํ•ด๊ฒฐ SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED]

by ๐Ÿงž‍โ™‚๏ธ 2020. 5. 26.
๋ฐ˜์‘ํ˜•
URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1076)>

PyTorch์—์„œ ResNet ์‹ ๊ฒฝ๋ง์„ ๋ถˆ๋Ÿฌ์˜ค๋‹ค๊ฐ€ ์ ‘ํ•œ ์—๋Ÿฌ์ด๋‹ค.

ํšŒ์‚ฌ์—์„œ ์ž‘์—…์„ ํ•˜๋‹ค๋ณด๋ฉด, SSL์ธ์ฆ์„œ ๋ฌธ์ œ๋กœ url์ ‘๊ทผ์— ๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ด์ „์—๋„ ์žˆ์—ˆ๋‹ค.

2020/04/14 - [IT-Engineering] - git push -u origin master ์‹คํ–‰์‹œ SSL certificate problem: self signed certificate in certificate chain ์—๋Ÿฌ ๋ฐœ์ƒ ํ•ด๊ฒฐ

 

git push -u origin master ์‹คํ–‰์‹œ SSL certificate problem: self signed certificate in certificate chain ์—๋Ÿฌ ๋ฐœ์ƒ ํ•ด๊ฒฐ

GitHub์— Repository๋ฅผ ๋งŒ๋“ค๊ณ  ์ž‘์€ ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋‚˜ ์˜ฌ๋ฆฌ๋ ค๋Š”๋ฐ, ์‚ฌ์†Œํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ์•ฝ๊ฐ„ ํ—ค๋งธ์–ด์„œ ๊ฒฝํ—˜์„ ๊ณต์œ ํ•œ๋‹ค. git push -u origin master ๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ SSL certificate problem: self..

gentlesark.tistory.com

๋งจ ์•„๋ž˜์˜ ๋กœ๊ทธ๊ฐ€ ์ „์ฒด๋กœ๊ทธ์ด๋‹ค.

๊ฒฐ๋ก ์ ์œผ๋กœ, urlopenํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์ƒ๊ธด ์—๋Ÿฌ์˜€๋‹ค.

์ด ๊ฒฝ์šฐ๋Š” PyTorch torch ํŒจํ‚ค์ง€์˜ hub.py์—์„œ download_url_to_file์„ ํ˜ธ์ถœํ•˜๋ฉด์„œ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ์˜€๋‹ค.

์„ค์น˜๋œ ํŒจํ‚ค์ง€ ์ฝ”๋“œ๋ฅผ ๊ฑด๋“œ๋ฆฐ๋‹ค๋Š” ๊ฒƒ์ด ์ฐœ์ฐœํ•˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ˆ˜์ •์„ ๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

{conda python๊ฒฝ๋กœ}/site-packages/torch/hub.py ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ–ˆ๋‹ค.

ํ•ด๋‹น ์ฝ”๋“œ๋Š” ์ด๋Ÿฐ ํ˜•ํƒœ์˜€๋Š”๋ฐ

u = urlopen(url)

์•„๋ž˜์™€ ๊ฐ™์ด ์ ๋‹นํ•œ ์œ„์น˜์— sslํŒจํ‚ค์ง€๋ฅผ importํ•ด์ฃผ๊ณ 

import ssl

์œ„์˜ urlopen์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ด์ฃผ๋ฉด, ssl์ธ์ฆ ๋ฌธ์ œ๋ฅผ ๋„˜๊ธธ ์ˆ˜ ์žˆ๋‹ค.

context = ssl._create_unverified_context()
u = urlopen(url,context=context)

๋” ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์žˆ์œผ๋ฉด ๊ณต์œ ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ๋‹ค.

python ํŒจํ‚ค์ง€๋“ค์˜ ํ•จ์ˆ˜๊ฐ€ ssl์ธ์ฆ์„œ๋ฅผ ํ•„์š”๋กœ ํ•  ๊ฒฝ์šฐ ๋งค๋ฒˆ ์ด๋ ‡๊ฒŒ ์ˆ˜์ •ํ•ด์ฃผ์–ด์•ผ ํ• ๋ ค๋‚˜ ์‹ถ๋‹ค ใ… ใ… 

---------------------------------------------------------------------------
SSLCertVerificationError                  Traceback (most recent call last)
~/anaconda3/envs/ai/lib/python3.7/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1318                 h.request(req.get_method(), req.selector, req.data, headers,
-> 1319                           encode_chunked=req.has_header('Transfer-encoding'))
   1320             except OSError as err: # timeout error

~/anaconda3/envs/ai/lib/python3.7/http/client.py in request(self, method, url, body, headers, encode_chunked)
   1251         """Send a complete request to the server."""
-> 1252         self._send_request(method, url, body, headers, encode_chunked)
   1253 

~/anaconda3/envs/ai/lib/python3.7/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1297             body = _encode(body, 'body')
-> 1298         self.endheaders(body, encode_chunked=encode_chunked)
   1299 

~/anaconda3/envs/ai/lib/python3.7/http/client.py in endheaders(self, message_body, encode_chunked)
   1246             raise CannotSendHeader()
-> 1247         self._send_output(message_body, encode_chunked=encode_chunked)
   1248 

~/anaconda3/envs/ai/lib/python3.7/http/client.py in _send_output(self, message_body, encode_chunked)
   1025         del self._buffer[:]
-> 1026         self.send(msg)
   1027 

~/anaconda3/envs/ai/lib/python3.7/http/client.py in send(self, data)
    965             if self.auto_open:
--> 966                 self.connect()
    967             else:

~/anaconda3/envs/ai/lib/python3.7/http/client.py in connect(self)
   1421             self.sock = self._context.wrap_socket(self.sock,
-> 1422                                                   server_hostname=server_hostname)
   1423 

~/anaconda3/envs/ai/lib/python3.7/ssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session)
    422             context=self,
--> 423             session=session
    424         )

~/anaconda3/envs/ai/lib/python3.7/ssl.py in _create(cls, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, context, session)
    869                         raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
--> 870                     self.do_handshake()
    871             except (OSError, ValueError):

~/anaconda3/envs/ai/lib/python3.7/ssl.py in do_handshake(self, block)
   1138                 self.settimeout(None)
-> 1139             self._sslobj.do_handshake()
   1140         finally:

SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1076)

During handling of the above exception, another exception occurred:

URLError                                  Traceback (most recent call last)
<ipython-input-3-489dfb92ded8> in <module>
----> 1 model_ft = resnet18(pretrained=True)

<ipython-input-1-55f0ae24cc34> in resnet18(pretrained, progress, **kwargs)
    239     """
    240     return _resnet('resnet18', BasicBlock, [2, 2, 2, 2], pretrained, progress,
--> 241                    **kwargs)
    242 
    243 

<ipython-input-1-55f0ae24cc34> in _resnet(arch, block, layers, pretrained, progress, **kwargs)
    225     if pretrained:
    226         state_dict = load_state_dict_from_url(model_urls[arch],
--> 227                                               progress=progress)
    228         model.load_state_dict(state_dict)
    229     return model

~/anaconda3/envs/ai/lib/python3.7/site-packages/torch/hub.py in load_state_dict_from_url(url, model_dir, map_location, progress, check_hash)
    497         sys.stderr.write('Downloading: "{}" to {}\n'.format(url, cached_file))
    498         hash_prefix = HASH_REGEX.search(filename).group(1) if check_hash else None
--> 499         download_url_to_file(url, cached_file, hash_prefix, progress=progress)
    500 
    501     # Note: extractall() defaults to overwrite file if exists. No need to clean up beforehand.

~/anaconda3/envs/ai/lib/python3.7/site-packages/torch/hub.py in download_url_to_file(url, dst, hash_prefix, progress)
    396     #context = ssl._create_unverified_context()
    397     #u = urlopen(url,context=context)
--> 398     u = urlopen(url)
    399     meta = u.info()
    400     if hasattr(meta, 'getheaders'):

~/anaconda3/envs/ai/lib/python3.7/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    220     else:
    221         opener = _opener
--> 222     return opener.open(url, data, timeout)
    223 
    224 def install_opener(opener):

~/anaconda3/envs/ai/lib/python3.7/urllib/request.py in open(self, fullurl, data, timeout)
    523             req = meth(req)
    524 
--> 525         response = self._open(req, data)
    526 
    527         # post-process response

~/anaconda3/envs/ai/lib/python3.7/urllib/request.py in _open(self, req, data)
    541         protocol = req.type
    542         result = self._call_chain(self.handle_open, protocol, protocol +
--> 543                                   '_open', req)
    544         if result:
    545             return result

~/anaconda3/envs/ai/lib/python3.7/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    501         for handler in handlers:
    502             func = getattr(handler, meth_name)
--> 503             result = func(*args)
    504             if result is not None:
    505                 return result

~/anaconda3/envs/ai/lib/python3.7/urllib/request.py in https_open(self, req)
   1360         def https_open(self, req):
   1361             return self.do_open(http.client.HTTPSConnection, req,
-> 1362                 context=self._context, check_hostname=self._check_hostname)
   1363 
   1364         https_request = AbstractHTTPHandler.do_request_

~/anaconda3/envs/ai/lib/python3.7/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1319                           encode_chunked=req.has_header('Transfer-encoding'))
   1320             except OSError as err: # timeout error
-> 1321                 raise URLError(err)
   1322             r = h.getresponse()
   1323         except:

URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1076)>
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€