You need to enable JavaScript to run this app. 求助:Python ftplib下载FTP文件报ConnectionRefusedError
优惠活动
大模型
产品
解决方案
定价
更多

求助:Python ftplib下载FTP文件报ConnectionRefusedError

ftplib 下载文件触发 ConnectionRefusedError 问题记录

使用Python ftplib 库从FTP服务器拉取文件时,遇到 ConnectionRefusedError: [Errno 111] Connection refused 报错。
实现代码如下:

import os
import sys
import configparser
import socket

def downloadfile(downftp, remotepath,localpath):
    bufsize=1024
    fp = open(localpath,'wb')     
    downftp.set_debuglevel(2)
    downftp.retrbinary('RETR ' + remotepath, fp.write, bufsize)
    fp.close()   
    
def ftpconnect(host,username,password):
    ftp = FTP()
    ftp.connect(host=host,port=10022)
    ftp.login(username, password)
    return ftp

def uploadfile(ftp,remotepath, localpath):
    bufsize=1024
    fp=open(localpath,'rb')       
    ftp.set_debuglevel(0)
    ftp.storbinary('STOR ' + remotepath, fp, bufsize)
    fp.close()
conftp = ftpconnect('42.*.**.201','cityof********.****erver','COU***123')
downloadfile(conftp,'/world/datapacks/sys/data/pgdc/functions/food.mcfunction','food.mcfunction')

完整报错堆栈

File "<pyshell#7>", line 1, in <module>
    ftp.dir()
  File "/usr/lib/python3.6/ftplib.py", line 575, in dir
    self.retrlines(cmd, func)
  File "/usr/lib/python3.6/ftplib.py", line 468, in retrlines
    with self.transfercmd(cmd) as conn, \
  File "/usr/lib/python3.6/ftplib.py", line 399, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "/usr/lib/python3.6/ftplib.py", line 361, in ntransfercmd
    source_address=self.source_address)
  File "/usr/lib/python3.6/socket.py", line 724, in create_connection
    raise err
  File "/usr/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

已尝试的排查动作

  • 执行 ftp.set_pasv(True) 开启被动模式,程序仍抛出相同的 ConnectionRefusedError: [Errno 111] Connection refused 错误
  • 执行 ftp.set_pasv(False) 关闭被动模式(切换为主动模式),程序会卡在以下日志输出状态无响应:
...
*cmd* 'RETR /world/datapacks/sys/data/pgdc/functions/food.mcfunction'
*put* 'RETR /world/datapacks/sys/data/pgdc/functions/food.mcfunction\r\n'
*get* '150 Opening ASCII mode data connection\n'
*resp* '150 Opening ASCII mode data connection'

需求

获取该场景对应的排查思路与解决方法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.22 16:57:04