Skip to content

Commit

Permalink
Added download from ftp server
Browse files Browse the repository at this point in the history
  • Loading branch information
nickssl committed Jan 5, 2024
1 parent a500f0c commit 4fc3919
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyspedas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from .utilities.datasets import find_datasets
# Note: "download" and "download_file" might be problematic names to import, due to risk of conflict with other packages
from .utilities.download import download, download_file, check_downloaded_file
from .utilities.download_ftp import download_ftp
from .utilities.interpol import interpol
from .utilities.leap_seconds import load_leap_table
from .utilities.libs import libs
Expand Down
46 changes: 46 additions & 0 deletions pyspedas/utilities/download_ftp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from ftplib import FTP
import os


def download_ftp(
ftp_server,
ftp_path,
filename,
local_dir,
username="anonymous",
password="anonymous@",
):
"""
Download a file from an FTP server.
Parameters:
ftp_server (str):
FTP server address.
ftp_path (str):
Path on the FTP server where the file is located.
filename (str):
Name of the file to download.
local_dir (str):
Local directory to save the file.
username (str):
Username for the FTP server. Default is 'anonymous'.
password (str):
Password for the FTP server. Default is 'anonymous@'.
Returns:
list of str:
List of files downloaded.
"""
return_files = []
with FTP(ftp_server) as ftp:
ftp.login(user=username, passwd=password)
ftp.cwd(ftp_path) # Change to the directory containing the file

local_filename = os.path.join(local_dir, filename)
with open(local_filename, "wb") as local_file:
ftp.retrbinary("RETR " + filename, local_file.write)

print(f"File '{filename}' downloaded successfully to '{local_dir}'")
return_files.append(local_filename)

return return_files

0 comments on commit 4fc3919

Please sign in to comment.