본문 바로가기
Python

파이썬 SSL 오류 발생시

by 앗사비 2023. 7. 12.
728x90

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1002)

from OpenSSL import SSL
from socket import socket
from datetime import datetime

def get_ssl_expiry_datetime(hostname: str) -> datetime:
    ssl_socket = SSL.Connection(SSL.Context(SSL.SSLv23_METHOD), socket())
    ssl_socket.connect((hostname, 443))
    ssl_socket.do_handshake()
    cert = ssl_socket.get_peer_certificate()
    expiry_date = datetime.strptime(cert.get_notAfter().decode('ascii'), '%Y%m%d%H%M%SZ')
    return expiry_date

websites = ["google.com", "github.com", "facebook.com"]

for site in websites:
    try:
        print(f"{site} SSL Certificate Expiry Date: {get_ssl_expiry_datetime(site)}")
    except Exception as e:
        print(f"Could not retrieve SSL certificate for {site}. Error: {str(e)}")

 

 

---

 

urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='....com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: DH_KEY_TOO_SMALL] dh key too small (_ssl.c:1000)')))

 

import requests
import ssl

url = 'https://naver.com/'

class TLSAdapter(requests.adapters.HTTPAdapter):
    def init_poolmanager(self, *args, **kwargs):
        ctx = ssl.create_default_context()
        ctx.set_ciphers('DEFAULT@SECLEVEL=1')
        kwargs['ssl_context'] = ctx
        return super(TLSAdapter, self).init_poolmanager(*args, **kwargs)

with requests.Session() as session:
    session.mount('https://', TLSAdapter())
    response = session.get(url)
    print(response.text)

https://stackoverflow.com/questions/61631955/python-requests-ssl-error-during-requests

728x90

'Python' 카테고리의 다른 글

yield 한 방에 이해하기  (0) 2023.08.04
edge-tts 사용해보기  (0) 2023.07.21
amazon linux 2 - 파이썬 3.11 설치  (0) 2023.07.10
tkinter - 입력란 내용 없으면 버튼 비활성화  (0) 2023.05.11
판다스 치트시트  (0) 2023.04.27