You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用Frankfurter API出现InvalidChunkLength错误如何解决?

Frankfurter汇率API请求报错解决(InvalidChunkLength/ChunkedEncodingError)

问题情况

使用Python调用Frankfurter汇率API时,打印状态码时触发InvalidChunkLength或ChunkedEncodingError错误,但直接请求api.frankfurter.app能正常返回200状态码。运行环境为Windows 11下的Python虚拟环境,本人为Python新手。

代码示例

import requests

convert_from = str(input("Enter the currency to convert from: ")).upper()
convert_to =  str(input("Enter the currency to convert to: ")).upper()
convert_amount= float(input("Enter the amount that you want to convert: "))
host = 'api.frankfurter.app'
response = requests.get(f'https://{host}/latest?amount={convert_amount}&from={convert_from}&to={convert_to}')

print(response.status_code)

报错信息

Traceback (most recent call last):
  File "C:\Users\Mysystem\Desktop\PYTHON\MyProjects\currency\lib\site-packages\urllib3\response.py", line 748, in _update_chunk_length
    self.chunk_left = int(line, 16)
ValueError: invalid literal for int() with base 16: b''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Mysystem\Desktop\PYTHON\MyProjects\currency\lib\site-packages\urllib3\response.py", line 443, in _error_catcher
    yield
  File "C:\Users\Mysystem\Desktop\PYTHON\MyProjects\currency\lib\site-packages\urllib3\response.py", line 815, in read_chunked
    self._update_chunk_length()
  File "C:\Users\Mysystem\Desktop\PYTHON\MyProjects\currency\lib\site-packages\urllib3\response.py", line 752, in _update_chunk_length
    raise InvalidChunkLength(self, line)
urllib3.exceptions.InvalidChunkLength: InvalidChunkLength(got length b'', 0 bytes read)

已尝试的方法

尝试过针对ChunkedEncodingError的常规解决方案,但无效。

可行解决建议

1. 关闭持久连接或禁用分块传输

在请求中添加Connection: close头,避免持久连接引发的分块问题:

response = requests.get(
    f'https://{host}/latest?amount={convert_amount}&from={convert_from}&to={convert_to}',
    headers={'Connection': 'close'}
)

或者强制关闭流传输:

response = requests.get(
    f'https://{host}/latest?amount={convert_amount}&from={convert_from}&to={convert_to}',
    stream=False
)

2. 更新依赖库

虚拟环境中更新requests和urllib3到最新版本,修复已知的传输问题:

pip install --upgrade requests urllib3

3. 添加请求重试机制

针对不稳定的网络连接,使用重试适配器确保请求失败时自动重试:

import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

# 配置会话和重试策略
session = requests.Session()
retry_strategy = Retry(
    total=3,  # 重试次数
    backoff_factor=1,  # 重试间隔递增
    status_forcelist=[429, 500, 502, 503, 504]  # 需要重试的状态码
)
adapter = HTTPAdapter(max_retries=retry_strategy)
session.mount('https://', adapter)

# 发起请求
convert_from = str(input("Enter the currency to convert from: ")).upper()
convert_to =  str(input("Enter the currency to convert to: ")).upper()
convert_amount= float(input("Enter the amount that you want to convert: "))
host = 'api.frankfurter.app'
response = session.get(f'https://{host}/latest?amount={convert_amount}&from={convert_from}&to={convert_to}')

print(response.status_code)

4. 检查网络代理

如果系统开启了代理服务,可能干扰分块传输,尝试关闭代理后重新测试请求。


内容的提问来源于stack exchange,提问作者AnatuGreen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 15:20:44