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

使用Paramiko连接SFTP出现BufferError问题求助

问题

我使用以下Python代码通过Paramiko连接SFTP服务器拉取文件,这是一段基础的SFTP连接代码:

def get_fund_files():
    host, port = "ftp.example.com", 22
    transport = paramiko.Transport((host, port))
    # Auth
    username, password = "user", "password"
    transport.connect(None, username, password)
    # Go!
    with paramiko.SFTPClient.from_transport(transport) as sftp:

目前这段代码在我的Bitnami Ubuntu服务器上突然无法运行,出现如下错误:

Traceback (most recent call last):
  File "main.py", line 288, in <module>
    main()
  File "main.py", line 15, in main
    get_fund_files()
  File "main.py", line 28, in get_fund_files
    transport.connect(None, username, password)
  File "/usr/lib/python3/dist-packages/paramiko/transport.py", line 1263, in connect
    self.auth_password(username, password)
  File "/usr/lib/python3/dist-packages/paramiko/transport.py", line 1431, in auth_password
    self.auth_handler.auth_password(username, password, my_event)
  File "/usr/lib/python3/dist-packages/paramiko/auth_handler.py", line 138, in auth_password
    self._request_auth()
  File "/usr/lib/python3/dist-packages/paramiko/auth_handler.py", line 189, in _request_auth
    self.transport._send_message(m)
  File "/usr/lib/python3/dist-packages/paramiko/transport.py", line 1766, in _send_message
    self.packetizer.send_message(data)
  File "/usr/lib/python3/dist-packages/paramiko/packet.py", line 399, in send_message
    out = self.__block_engine_out.update(packet)
  File "/usr/lib/python3/dist-packages/cryptography/hazmat/primitives/ciphers/base.py", line 149, in update
    return self._ctx.update(data)
  File "/usr/lib/python3/dist-packages/cryptography/hazmat/backends/openssl/ciphers.py", line 124, in update
    n = self.update_into(data, buf)
  File "/usr/lib/python3/dist-packages/cryptography/hazmat/backends/openssl/ciphers.py", line 140, in update_into
    self._backend._ffi.from_buffer(data, require_writable=True), len(data)
BufferError: Object is not writable.

相同代码在克隆实例上运行正常,且服务器和SFTP无已知变更,错误发生在transport.connect(None, username, password)步骤。

排查与解决建议
  • 对齐依赖库版本:对比克隆实例和故障服务器的paramiko、cryptography版本,运行pip list | grep -E "paramiko|cryptography"查看版本号,将故障服务器的库版本同步到克隆实例的版本,比如执行pip install cryptography==x.x.x paramiko==x.x.x(替换为实际匹配的版本号)。
  • 修复或重装依赖:若存在库文件损坏,先卸载现有库:pip uninstall -y paramiko cryptography,再重新安装;如果是系统级Python依赖,用apt reinstall python3-paramiko python3-cryptography修复系统包。
  • 检查OpenSSL兼容性:运行openssl version对比两个实例的OpenSSL版本,若差异较大,尝试升级或降级到兼容版本(操作前注意系统稳定性)。
  • 添加调试日志:在代码开头加入日志配置,获取加密协商阶段的详细信息:
    import logging
    logging.basicConfig(level=logging.DEBUG)
    
  • 指定加密算法:手动设置兼容的加密算法集合,避免协商到有问题的算法:
    transport = paramiko.Transport((host, port))
    transport.set_security_options(ciphers=['aes256-ctr', 'aes192-ctr', 'aes128-ctr'])
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 20:09:25