Skip to content

Commit 2180893

Browse files
committed
add ftp
1 parent 5763e7a commit 2180893

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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

agentstack/tools/ftp.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "ftp",
3+
"package": "",
4+
"env": "FTP_HOST=...\nFTP_USER=...\nFTP_PASSWORD=...",
5+
"tools": ["upload_files"]
6+
}

0 commit comments

Comments
 (0)