Skip to content

Commit

Permalink
Add the hacky solution for SSL: DH_KEY_TOO_SMALL error (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Apr 5, 2024
1 parent adf71bf commit 3987723
Show file tree
Hide file tree
Showing 2 changed files with 20 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
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

0.8.2 (2024-04-05)
------------------

- Add the updated solution for the "ssl.SSLError: [SSL: DH_KEY_TOO_SMALL] dh key too small" error

0.8.1 (2024-03-31)
------------------

Expand Down

0 comments on commit 3987723

Please sign in to comment.