0
votes

I was trying use ftplib to download from a FTP server, but I get ConnectionRefusedError: [Errno 111] Connection refused This is my code:

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

I tried ftp.set_pasv(False), and it hanging on

...
*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.

1

1 Answers

0
votes

Are you able to successfully connect to the ftp server from command line i.e. without the python code ? I think this should be first step to check if Connection is Refused by ftp or something wrong with python code.

 ftp = FTP(host=ftp_host, user=ftp_user,passwd=ftp_pass)
 ftp.login()

Is your upload working fine ?