File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ from ftplib import FTP
2+ from crewai_tools import tool
3+ from dotenv import load_dotenv
4+ import os
5+
6+ load_dotenv ()
7+
8+ # FTP server details
9+ ftp_host = os .getenv ('FTP_HOST' )
10+ ftp_user = os .getenv ('FTP_USER' )
11+ ftp_p = os .getenv ("FTP_PASSWORD" )
12+ ftp_path = '/'
13+
14+
15+ @tool
16+ def upload_files (file_paths : list [str ]):
17+ """Upload a list of files to the FTP server."""
18+
19+ result = True
20+ # Loop through each file path in the list
21+ for file_path in file_paths :
22+ # Establish FTP connection
23+ with FTP (ftp_host ) as ftp :
24+ try :
25+ # Login to the server
26+ ftp .login (user = ftp_user , passwd = ftp_p )
27+ print (f"Connected to FTP server: { ftp_host } " )
28+
29+ # Change to the desired directory on the server
30+ ftp .cwd (ftp_path )
31+
32+ # Open the file in binary mode for reading
33+ with open (file_path , 'rb' ) as file :
34+ # Upload the file
35+ ftp .storbinary (f'STOR { file_path } ' , file )
36+ print (f"Successfully uploaded { file_path } to { ftp_path } " )
37+
38+ except Exception as e :
39+ print (f"An error occurred: { e } " )
40+ result = False
41+
42+ return result
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " ftp" ,
3+ "package" : " " ,
4+ "env" : " FTP_HOST=...\n FTP_USER=...\n FTP_PASSWORD=..." ,
5+ "tools" : [" upload_files" ]
6+ }
You can’t perform that action at this time.
0 commit comments