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

使用D1 Mini通过MicroPython urequests调用Google Apps Script遇TLS溢出错误

D1 Mini + MicroPython urequests调用Google Apps Script出现TLS缓冲区溢出错误

使用D1 Mini通过MicroPython的urequests库请求Google Apps Script接口时,触发如下TLS缓冲区溢出错误:

>>> %Run -c $EDITOR_CONTENT
TLS buffer overflow, record size: 6682 (+5)
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "urllib/urequest.py", line 32, in urlopen
OSError: (-257, 'RECORD_OVERFLOW')
>>> 

运行的代码如下:

import urllib.urequest
f = urllib.urequest.urlopen('script.google.com/macros/s/…')
# f.code
# f.headers
c=f.read()
c.decode("utf-8")[3:6]

解决方法

  • 补全HTTPS协议头:原URL缺少https://前缀,会导致请求解析异常,先修正为完整的HTTPS地址。

  • 调大TLS缓冲区:ESP8266(D1 Mini基于该芯片)的MicroPython默认TLS缓冲区较小,可手动创建SSL上下文并调整缓冲区大小:

import urllib.urequest
import ssl

# 创建SSL上下文并调整缓冲区
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
# 设置更大的缓冲区,适配返回数据的大小
ctx.set_option(ssl.OP_TLS_BLOCK_PADDING_BUG, 16384)

# 使用调整后的上下文发起请求
f = urllib.urequest.urlopen('https://script.google.com/macros/s/…', context=ctx)
c = f.read()
result = c.decode("utf-8")[3:6]
  • 分块读取响应:避免一次性读取全部响应内容,改用分块读取的方式降低内存占用:
import urllib.urequest

f = urllib.urequest.urlopen('https://script.google.com/macros/s/…')
chunk_size = 1024
content = b''
# 分块读取直到结束
while True:
    chunk = f.read(chunk_size)
    if not chunk:
        break
    content += chunk
result = content.decode("utf-8")[3:6]
  • 精简GAS返回内容:如果Google Apps Script返回的数据量过大,修改脚本只返回必要的字段,减少传输的数据体积,从根源避免缓冲区溢出。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 23:55:26