[英]use python ftplib downloading but get ConnectionRefusedError: [Errno 111] Connection refused
I was trying use ftplib to download from a FTP server, but I get ConnectionRefusedError: [Errno 111] Connection refused
This is my code:我正在尝试使用 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')
All error:所有错误:
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
I tried ftp.set_pasv(True)
,it shows the same error ConnectionRefusedError: [Errno 111] Connection refused
我试过
ftp.set_pasv(True)
,它显示相同的错误ConnectionRefusedError: [Errno 111] Connection refused
I tried ftp.set_pasv(False)
, and it hanging on我试过
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'
Help please.Thank you.请帮助。谢谢。
Are you able to successfully connect to the ftp server from command line ie without the python code?您是否能够从命令行成功连接到 ftp 服务器,即没有 python 代码? I think this should be first step to check if Connection is Refused by ftp or something wrong with python code.
我认为这应该是检查连接是否被 ftp 拒绝或 python 代码有问题的第一步。
ftp = FTP(host=ftp_host, user=ftp_user,passwd=ftp_pass)
ftp.login()
Is your upload working fine?你的上传工作正常吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.