使用脚本从 ftp 检索数据

使用脚本从 ftp 检索数据
ftp 123.456
Name: anonymous
Password: password
binary
cd 001
get mfile.d.Z

我需要编写一个执行上述代码的脚本来自动检索数据。

答案1

这是一个简单的 Python 脚本:

#!/usr/bin/python

import ftplib

host = '127.0.0.1'
dir = '001'
filename = 'mdfile.d.z'

# connect

ftp = ftplib.FTP(host)

# login - anonymous by default
# use ftp.login("username", "password") if no anon access

ftp.login()

# change to desired dir

ftp.cwd(dir)

# fetch the file, and copy it to a file of the
# same name in the local pwd

with open(filename, 'wb') as local_file:
    ftp.retrbinary("RETR {}".format(filename), local_file.write) 

相关内容