requests 사용을 통한 개발 과정에서 SSLError가 발생했다.

 

Fixing SSLV3_ALERT_HANDSHAKE_FAILURE with urllib3 2.0

Over the years, the urllib3 HTTP client maintained its SSL cipher suites based on Hynek's evergreen list. However, with the release of urllib3 version 2.0, we decided to require OpenSSL 1.1.1 or above and rely on the default cipher suites, which are secure

quentin.pradet.me

로컬 및 개발 환경에서는 문제가 발생하지 않았습니다. 

3.9x 버전에서 verify=False 인자를 추가하는 방법은 파이썬 버전을 내려야 하기 때문에 해결 방법이 아니었습니다.

 

urllib3 라이브러리 사용을 통해 문제를 해결했습니다.

requests보다 더 Low Level 코드라고 합니다.

from urllib3 import PoolManager

if environment == "PROD":
    from urllib3.util import create_urllib3_context

    ctx = create_urllib3_context()
    ctx.load_default_certs()
    ctx.set_ciphers('AES256-GCM-SHA384')
    pool_manager = PoolManager(ssl_context=ctx)
else:
    pool_manager = PoolManager()

 

POST 예제 코드

post_data ={
    "key_a": "val_a",
    "key_b": "val_b",
    "key_c": "val_c",
}

try:
    with pool_manager as pool:
        response = pool.request(
            method='POST',
            url=req_url,
            body=json.dumps(post_data).encode('utf-8'),
            headers={
                "Content-Type": "application/json;charset=utf-8"
            },
            timeout=5.0,
        )
except urllib3.exceptions.RequestError as e:
    print(f"urllib3 error: {e}")
    
# x-www-form-urlencoded 형식
# from urllib.parse import urlencode

# with pool_manager as pool:
#     response = pool.request(
#         method='POST',
#         url=req_url,
#         body=urlencode(post_data),
#         headers={
#             "Content-Type": "application/x-www-form-urlencoded"
#         },
#         timeout=5.0,
#     )

if response.status != 200:
    raise ...
...
resp = response.json()

 

GET 예제 코드

params ={
    "key_a": "val_a",
    "key_b": "val_b",
    "key_c": "val_c",
}

with pool_manager as pool:
    response = pool.request(
        method='GET',
        url=req_url,
        fields=params,
    )

if response.status != 200:
    raise ...
....
resp = response.json()

+ Recent posts