1. ホーム
  2. python

python encountered ValueError: check_hostname requires server_hostname 解決法

2022-02-12 05:21:42
<パス

記事目次

問題発生

ValueError: check_hostnameはserver_hostnameを必要とします。

具体的に報告されたエラーは以下の通りです。

Traceback (most recent call last):
  File "pythonrepos.py", line 6, in <module>
    r = requests.get(url, headers=headers)
  File "C:\Users\m1521\AppData\Roaming\Python\Python38\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\m1521\AppData\Roaming\Python\Python38\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\m1521\AppData\Roaming\Python\Python38\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\m1521\AppData\Roaming\Python\Python38\site-packages\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\m1521\AppData\Roaming\Python\Python38\site-packages\requests\adapters.py", line 439, in send
    resp = conn.urlopen(
  File "C:\Users\m1521\AppData\Roaming\Python\Python\Python38\site-packages\urllib3\connectionpool.py", line 696, in urlopen
    self._prepare_proxy(conn)
  File "C:\Users\m1521\AppData\Roaming\Python\Python38\site-packages\urllib3\connectionpool.py", line 964, in _prepare_proxy
    conn.connect()
  File "C:\Users\m1521\AppData\Roaming\Python\Python38\site-packages\urllib3\connection.py", line 359, in connect
    conn = self._connect_tls_proxy(hostname, conn)
  File "C:\Users\m1521\AppData\Roaming\Python\Python38\site-packages\urllib3\connection.py", line 500, in _connect_tls_proxy
    return ssl_wrap_socket(
  File "C:\Users\m1521\AppData\Roaming\Python\Python38\site-packages\urllib3\util\ssl_.py", line 432, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
  File "C:\Users\m1521\AppData\Roaming\Python\Python38\site-packages\urllib3\util\ssl_.py", line 474, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock)
  File "D:\Program Files\Python38\lib\ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "D:\Program Files\Python38\lib\ssl.py", line 997, in _create
    raise ValueError("check_hostname requires server_hostname")
ValueError: check_hostname requires server_hostname


上記のエラーを発生させる、実行されたコードは以下の通りです。

import requests

# Execute the API call and store the response
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")

# Assign the API response to a variable
response_dict = r.json()

# Process the result
print(response_dict.keys())


解決方法

この問題の原因は、urllib3のバージョンが高すぎることと、新しいバージョンのurllib3ではいくつかの点が変更されたため、エラーを報告することです。以下の解決策は、urlib3のバージョンを下げることです。

コマンドラインを開き、実行します。

pip install urllib3==1.25.11


成功すれば終了、失敗すれば次のようなエラーが報告されますので、読み進めてください。

Defaulting to user installation because normal site-packages is not writable
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, ' EOF occurred in violation of protocol (_ssl.c:1125)')': /simple/urllib3/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, ' EOF occurred in violation of protocol (_ssl.c:1125)')': /simple/urllib3/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, ' EOF occurred in violation of protocol (_ssl.c:1125)')': /simple/urllib3/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, ' EOF occurred in violation of protocol (_ssl.c:1125)')': /simple/urllib3/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, ' EOF occurred in violation of protocol (_ssl.c:1125)')': /simple/urllib3/
Could not fetch URL https://pypi.org/simple/urllib3/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/urllib3/ (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125) '))) - skipping
ERROR: Could not find a version that satisfies the requirement urllib3==1.25.11
ERROR: No matching distribution found for urllib3==1.25.11



を使用することが可能です。

pip install urllib3==1.25.11 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com


高速で接続の問題が発生しない国内ソース経由でのインストールを意味します。

インストール成功の表示

PS D:\user\ documentation\python\python_work\data_visualization\download_data> pip install urllib3==1.25.11 -i http://pypi.douban.com/simple -- trusted-host pypi.douban.com
>>
Defaulting to user installation because normal site-packages is not writable
Looking in indexes: http://pypi.douban.com/simple
Collecting urllib3==1.25.11
  Downloading http://pypi.doubanio.com/packages/56/aa/4ef5aa67a9a62505db124a5cb5262332d1d4153462eb8fd89c9fa41e5d92/urllib3-1.25.11- py2.py3-none-any.whl (127 kB)
     |████████████████████████████████| 127 kB 3.3 MB/s
Installing collected packages: urllib3
  Attempting uninstall: urllib3
    Found existing installation: urllib3 1.26.4
    Uninstalling urllib3-1.26.4:
      Successfully uninstalled urllib3-1.26.4
Successfully installed urllib3-1.25.11
PS D:\user\ documentation\python\python_work\data_visualization\download_data>


そして、自分のプログラムを実行すると、うまくいきます!

Status code: 200
dict_keys(['total_count', 'incomplete_results', 'items'])


参考

[1] https://stackoverflow.com/questions/66642705/why-requests-raise-this-exception-check-hostname-requires-server-hostname