Skip to content

Commit

Permalink
Add the hacky solution for SSL: DH_KEY_TOO_SMALL error
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Apr 5, 2024
1 parent adf71bf commit 1a9d39f
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions HinetPy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from multiprocessing.pool import ThreadPool

import requests
from requests.adapters import HTTPAdapter
from urllib3 import PoolManager
from urllib3.util import create_urllib3_context

from .header import NETWORK
from .utils import (
Expand All @@ -33,6 +36,16 @@
logger = logging.getLogger(__name__)


# Hacking solution for "ssl.SSLError: [SSL: DH_KEY_TOO_SMALL] dh key too small" error.
# Reference: https://stackoverflow.com/a/76217135
class AddedCipherAdapter(HTTPAdapter):
def init_poolmanager(self, connections, maxsize, block=False):
ctx = create_urllib3_context(ciphers=":HIGH:!DH:!aNULL")
self.poolmanager = PoolManager(
num_pools=connections, maxsize=maxsize, block=block, ssl_context=ctx
)


class BaseClient:
"""
Base client to login in the Hi-net website.
Expand Down Expand Up @@ -168,6 +181,8 @@ def login(self, user, password):
if len(password) > 12:
logger.warning("Password longer than 12 characters may be truncated.")
self.session = requests.Session()
self.session.mount(self._HINET, AddedCipherAdapter())
self.session.mount(self._AUTH, AddedCipherAdapter())
self.session.get(self._AUTH, timeout=self.timeout) # get cookie
resp = self.session.post(
self._AUTH,
Expand Down

0 comments on commit 1a9d39f

Please sign in to comment.