Ftplib doesn't send files in celery bot
I used a code from here to download files in Selenium Grid VMs, but, when I use FTP, it shows no error and it doesn't send the files.
Code from ftp bot
def is_connected(ftp):
try:
ftp.retrlines('LIST')
except (socket.timeout, OSError, AttributeError):
return False
return True
@shared_task()
def send_binary(file_name : str, file_path):
ftp = FTP(
FTP_FAT_HOST,
FTP_FAT_USER,
FTP_FAT_PASSWORD,
timeout=30
)
ftp.set_pasv(True)
ftp.encoding='utf-8'
if(not is_connected(ftp)):
ftp.login()
upload_to_ftp(file_path, ftp)
def upload_to_ftp(local_file, ftp : FTP):
try:
with open(local_file, 'rb') as file:
ftp.storbinary(f'STOR {os.path.basename(local_file)}', file)
except Exception as e:
print(f'Erro ao enviar {local_file}: {e}')
Code I use to download pdf e txt files from my sources
def __download_file(self, file_name: str, target_directory: str) -> None:
if not os.path.exists(target_directory):
os.makedirs(target_directory)
contents = self.driver.execute(Command.DOWNLOAD_FILE, {"name": file_name})["value"]["contents"]
zip_target_file = os.path.join(target_directory, f"{file_name}.zip")
with open(zip_target_file, "wb") as file:
file.write(base64.b64decode(contents))
with zipfile.ZipFile(zip_target_file, "r") as zip_ref:
zip_ref.extractall(target_directory)
os.remove(zip_target_file)
Does anyone know another way to make it work ?
I tried creating a bot in celery just to ftp, before this it was inside my main code. I tried change the ftp server, I tried change the ftp passiveness and timeout. Nothing changed.