96067


Google
Colab上から以下のサイトへPythonのrequestsパッケージを利用してhttpsリクエストを発行すると、`UNEXPECTED_EOF_WHILE_READING`
エラーが発生しました。

アクセスしようとしたサイト：  
<https://demo-kabuka.e-shiten.jp/e_api_v4r3/>

エラーが発生したコードは以下です。

[code]

    import requests
    
    response = requests.get(url='https://demo-kabuka.e-shiten.jp/e_api_v4r3/')
    display(response)
    display(response.content)
    
[/code]

以下はGoogle Colabでエラーが発生したときの実行結果です。

[code]

    ---------------------------------------------------------------------------
    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)')))
    
[/code]

一方、Shellのcurlでhttpsリクエストを発行したところ、正しくレスポンスを取得できました。(レスポンスはshift-
jisのため一部文字化けします。)

[code]

    !curl https://demo-kabuka.e-shiten.jp/e_api_v4r3/
    
[/code]

Google Colab以外のパソコンでは、Pythonのrequestsパッケージも正しく動作しているため、Google
Colab固有の問題かと思うのですが、原因を調べる方法など教えていただけないでしょうか？

requestsパッケージのバージョンは 2.31.0 でした。

[code]

    !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: me@kennethreitz.org
    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
    
[/code]


CC BY-SA 4.0
2023-08-28T10:46:27.963
96063
2023-08-28T23:55:19.213
2023-08-28T18:14:12.630
3060
35267
question
python python3 google-colaboratory
Google Colabで特定のサイトへhttpsリクエストを送信すると、UNEXPECTED_EOF_WHILE_READING エラーが発生する


エラーメッセージにはもう一つ関連するモジュール`urllib3`があり、そちらの版数が`新しい`ことが原因ですね。  
以下のように 1.xx系の最終版を指定して置き換えると動作するようになりました。

[code]

    !pip3 install -U urllib3==1.26.16
    
[/code]

結果はこうなりました。

[code]

    <Response [200]>
    b'<!DOCTYPE HTML>\n<!--\n Product:\n \tmfds (realtime stock price board application modules).\n \n Copyright:\n \tCopyright (C) 2000 GOODLOTS CO.,LTD. ALL RIGHT RESERVED.\n \n Revision:\n\t$Id: mfds_json_api_move.html 1062 2023-02-28 04:45:07Z yoshi $\n \n Function:\n \tMFDS, json-api HP menu module (v4.0-000).\n\n Create:\n\t2022.06.08\n\tY.Yoshizawa\n\n-->\n<html lang="ja">\n<head>\n\t<meta http-equiv="content-type"        content="text/html; charset=Shift_JIS">\n\t<meta http-equiv="content-style-type"  content="text/css">\n\t<meta http-equiv="content-script-type" content="text/javascript">\n  \t<script\ttype="text/javascript" src="jquery-3.3.1.min.js"></script>\n\t<link rel="stylesheet" type="text/css" href="mfds_json_api_sample.css" />\n\t<title>\x97\xa7\x89\xd4\x8f\xd8\x8c\x94\x81E\x82\x85\x8ex\x93X\x81E\x82`\x82o\x82h</title>\n</head>\n<body style="display:none;">\n\t<!-- page title begin. -->\n\t<table>\n\t<tr>\n\t\t<td><a href="mfds_json_api_menu.html"><img class="page_logo" /></a></td>\n\t\t<td><span class="page_title">\x97\xa7\x89\xd4\x8f\xd8\x8c\x94\x81E\x82\x85\x8ex\x93X\x81E\x82`\x82o\x82h\x90\xea\x97p\x83y\x81[\x83W</span></td>\n\t</tr>\n\t</table>\n\t<!-- page title end. -->\n\n\t<!-- page body begin. -->\n\t<hr>\n\t<br>\n\t<br>\n\t\x81@\x81@\x81@\x81@\x96{\x83y\x81[\x83W\x82\xcd\x88\xf8\x82\xc1\x89z\x82\xb5\x92v\x82\xb5\x82\xdc\x82\xb5\x82\xbd\x81B<br>\n\t<br>\n\t\x81@\x81@\x81@\x81@\x90V\x82\xb5\x82\xa2\x82t\x82q\x82k\x82\xcd<a href="https://www.e-shiten.jp/e_api/">\x82\xb1\x82\xbf\x82\xe7</a>\x82\xc5\x82\xb7\x81B<br>\n</body>\n<script>\n$(function ()\n{\n\t$(\'.page_logo\').attr (\'src\', \'logo_web.gif\');\n\t$(\'body\').css (\'display\', \'\');\n});\n</script>\n</html>\n'
    
[/code]


CC BY-SA 4.0
2023-08-28T23:55:19.213
96067
2023-08-28T23:55:19.213
26370
96063
answer