【问题标题】:Is there a way to solve an ssl error (DH key too small) on python when using pd.read_excel()?使用 pd.read_excel() 时,有没有办法解决 python 上的 ssl 错误(DH 密钥太小)?
【发布时间】:2021-02-04 19:00:10
【问题描述】:

我正在尝试读取可以通过 url 访问的 excel 表,并将其加载到 DataFrame 中。

我正在像这样使用 pd.read_excel:


url = "https://www.recope.go.cr/wp-content/uploads/2020/09/PRECIOS-HIST%C3%93RICOS-CONSUMIDOR-FINAL-1-2.xls"
df = pd.read_excel(url,header=[2,3,4],nrows=347)

我得到:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/urllib/request.py", line 1350, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/usr/local/lib/python3.7/http/client.py", line 1277, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/lib/python3.7/http/client.py", line 1323, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.7/http/client.py", line 1272, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.7/http/client.py", line 1032, in _send_output
    self.send(msg)
  File "/usr/local/lib/python3.7/http/client.py", line 972, in send
    self.connect()
  File "/usr/local/lib/python3.7/http/client.py", line 1447, in connect
    server_hostname=server_hostname)
  File "/usr/local/lib/python3.7/ssl.py", line 423, in wrap_socket
    session=session
  File "/usr/local/lib/python3.7/ssl.py", line 870, in _create
    self.do_handshake()
  File "/usr/local/lib/python3.7/ssl.py", line 1139, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: DH_KEY_TOO_SMALL] dh key too small (_ssl.c:1091)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/pandas/util/_decorators.py", line 299, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 336, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "/usr/local/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 1063, in __init__
    path=str(self._io), storage_options=storage_options
  File "/usr/local/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 939, in inspect_excel_format
    content_or_path, "rb", storage_options=storage_options, is_text=False
  File "/usr/local/lib/python3.7/site-packages/pandas/io/common.py", line 563, in get_handle
    storage_options=storage_options,
  File "/usr/local/lib/python3.7/site-packages/pandas/io/common.py", line 288, in _get_filepath_or_buffer
    req = urlopen(filepath_or_buffer)
  File "/usr/local/lib/python3.7/site-packages/pandas/io/common.py", line 194, in urlopen
    return urllib.request.urlopen(*args, **kwargs)
  File "/usr/local/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/local/lib/python3.7/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/usr/local/lib/python3.7/urllib/request.py", line 543, in _open
    '_open', req)
  File "/usr/local/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python3.7/urllib/request.py", line 1393, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/local/lib/python3.7/urllib/request.py", line 1352, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: DH_KEY_TOO_SMALL] dh key too small (_ssl.c:1091)>

我在this question 中看到了类似的内容,但我试图在我的答案代码中包含无济于事。我对此有点陌生,但由于找不到解决方案,我认为最好在这里问。有什么办法可以解决这个 ssl 错误?

【问题讨论】:

    标签: python pandas ssl


    【解决方案1】:

    我再次尝试了链接中的解决方案,它成功了,但我不得不将一些代码更改为:

    req = requests.get(url)
    df = pd.read_excel(req.content,header=[2,3,4],nrows=347)
    

    由于某种原因,添加请求使解决方案起作用。

    【讨论】:

    • 我使用了原始代码,它对我有用。它可能是密码集配置。检查它here
    • 嗨@PauloMarques,您发送的链接正是我在问题末尾所指的那个。正如我所提到的,单独的解决方案不起作用,我还必须在我的答案中添加代码(带有请求),然后它就起作用了。
    • 我的意思是我使用了这个pd.read_excel(url,header=[2,3,4],nrows=347),它对我有用。
    猜你喜欢
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 2012-12-24
    • 2011-04-22
    相关资源
    最近更新 更多