Google Colab上から以下のサイトへPythonのrequestsパッケージを利用してhttpsリクエストを発行すると、UNEXPECTED_EOF_WHILE_READING
エラーが発生しました。
アクセスしようとしたサイト:
https://demo-kabuka.e-shiten.jp/e_api_v4r3/
エラーが発生したコードは以下です。
import requests
response = requests.get(url='https://demo-kabuka.e-shiten.jp/e_api_v4r3/')
display(response)
display(response.content)
以下はGoogle Colabでエラーが発生したときの実行結果です。
---------------------------------------------------------------------------
SSLEOFError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, body, headers, retries, timeout, chunked, response_conn, preload_content, decode_content, enforce_content_length)
466 try:
--> 467 self._validate_conn(conn)
468 except (SocketTimeout, BaseSSLError) as e:
18 frames
/usr/local/lib/python3.10/dist-packages/urllib3/connectionpool.py in _validate_conn(self, conn)
1091 if conn.is_closed:
-> 1092 conn.connect()
1093
/usr/local/lib/python3.10/dist-packages/urllib3/connection.py in connect(self)
641
--> 642 sock_and_verified = _ssl_wrap_socket_and_match_hostname(
643 sock=sock,
/usr/local/lib/python3.10/dist-packages/urllib3/connection.py in _ssl_wrap_socket_and_match_hostname(sock, cert_reqs, ssl_version, ssl_minimum_version, ssl_maximum_version, cert_file, key_file, key_password, ca_certs, ca_cert_dir, ca_cert_data, assert_hostname, assert_fingerprint, server_hostname, ssl_context, tls_in_tls)
782
--> 783 ssl_sock = ssl_wrap_socket(
784 sock=sock,
/usr/local/lib/python3.10/dist-packages/urllib3/util/ssl_.py in ssl_wrap_socket(sock, keyfile, certfile, cert_reqs, ca_certs, server_hostname, ssl_version, ciphers, ssl_context, ca_cert_dir, key_password, ca_cert_data, tls_in_tls)
468
--> 469 ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname)
470 return ssl_sock
/usr/local/lib/python3.10/dist-packages/urllib3/util/ssl_.py in _ssl_wrap_socket_impl(sock, ssl_context, tls_in_tls, server_hostname)
512
--> 513 return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
/usr/lib/python3.10/ssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session)
512 # ctx._wrap_socket()
--> 513 return self.sslsocket_class._create(
514 sock=sock,
/usr/lib/python3.10/ssl.py in _create(cls, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, context, session)
1070 raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
-> 1071 self.do_handshake()
1072 except (OSError, ValueError):
/usr/lib/python3.10/ssl.py in do_handshake(self, block)
1341 self.settimeout(None)
-> 1342 self._sslobj.do_handshake()
1343 finally:
SSLEOFError: [SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1007)
During handling of the above exception, another exception occurred:
SSLError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, **response_kw)
789 # Make the request on the HTTPConnection object
--> 790 response = self._make_request(
791 conn,
/usr/local/lib/python3.10/dist-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, body, headers, retries, timeout, chunked, response_conn, preload_content, decode_content, enforce_content_length)
490 new_e = _wrap_proxy_error(new_e, conn.proxy.scheme)
--> 491 raise new_e
492
SSLError: [SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1007)
The above exception was the direct cause of the following exception:
MaxRetryError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
485 try:
--> 486 resp = conn.urlopen(
487 method=request.method,
/usr/local/lib/python3.10/dist-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, **response_kw)
843
--> 844 retries = retries.increment(
845 method, url, error=new_e, _pool=self, _stacktrace=sys.exc_info()[2]
/usr/local/lib/python3.10/dist-packages/urllib3/util/retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
514 reason = error or ResponseError(cause)
--> 515 raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type]
516
MaxRetryError: HTTPSConnectionPool(host='demo-kabuka.e-shiten.jp', port=443): Max retries exceeded with url: /e_api_v4r3/ (Caused by SSLError(SSLEOFError(8, '[SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1007)')))
During handling of the above exception, another exception occurred:
SSLError Traceback (most recent call last)
<ipython-input-40-1f9596a04e09> in <cell line: 3>()
1 import requests
2
----> 3 response = requests.get(url='https://demo-kabuka.e-shiten.jp/e_api_v4r3/')
4 display(response)
5 display(response.content)
/usr/local/lib/python3.10/dist-packages/requests/api.py in get(url, params, **kwargs)
71 """
72
---> 73 return request("get", url, params=params, **kwargs)
74
75
/usr/local/lib/python3.10/dist-packages/requests/api.py in request(method, url, **kwargs)
57 # cases, and look like a memory leak in others.
58 with sessions.Session() as session:
---> 59 return session.request(method=method, url=url, **kwargs)
60
61
/usr/local/lib/python3.10/dist-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
587 }
588 send_kwargs.update(settings)
--> 589 resp = self.send(prep, **send_kwargs)
590
591 return resp
/usr/local/lib/python3.10/dist-packages/requests/sessions.py in send(self, request, **kwargs)
701
702 # Send the request
--> 703 r = adapter.send(request, **kwargs)
704
705 # Total elapsed time of the request (approximately)
/usr/local/lib/python3.10/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
515 if isinstance(e.reason, _SSLError):
516 # This branch is for urllib3 v1.22 and later.
--> 517 raise SSLError(e, request=request)
518
519 raise ConnectionError(e, request=request)
SSLError: HTTPSConnectionPool(host='demo-kabuka.e-shiten.jp', port=443): Max retries exceeded with url: /e_api_v4r3/ (Caused by SSLError(SSLEOFError(8, '[SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1007)')))
一方、Shellのcurlでhttpsリクエストを発行したところ、正しくレスポンスを取得できました。(レスポンスはshift-jisのため一部文字化けします。)
!curl https://demo-kabuka.e-shiten.jp/e_api_v4r3/
Google Colab以外のパソコンでは、Pythonのrequestsパッケージも正しく動作しているため、Google Colab固有の問題かと思うのですが、原因を調べる方法など教えていただけないでしょうか?
requestsパッケージのバージョンは 2.31.0 でした。
!pip3 show requests
Name: requests
Version: 2.31.0
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: [email protected]
License: Apache 2.0
Location: /usr/local/lib/python3.10/dist-packages
Requires: certifi, charset-normalizer, idna, urllib3
Required-by: CacheControl, chromadb, community, earthengine-api, embedchain, fastai, folium, gcsfs, gdown, google-api-core, google-cloud-bigquery, google-cloud-storage, google-colab, gspread, gTTS, jira, kaggle, langchain, langsmith, moviepy, music21, openai, pandas-datareader, panel, pooch, posthog, pymystem3, python-keycloak, requests-oauthlib, requests-toolbelt, spacy, Sphinx, tensorboard, tensorflow-datasets, torchdata, torchtext, torchvision, tweepy, twilio, yfinance, youtube-transcript-api
本ページは引っ越し致しました。
,新しいURLはこちらです。
と出てきます。そのURLを使うと処理は正常に行われるようですので、特に理由が無ければ新しいURLを使ってみてはどうでしょう?