如何使用Python在 FTP 服务器中下载和上传文件?
先决条件: FTP , ftplib
在这里,我们将学习如何使用Python在 FTP 服务器中下载和上传文件。在开始之前,首先我们要了解什么是FTP 。
FTP(文件传输协议)
文件传输协议 (FTP) 是一种应用层协议,用于在本地和远程文件系统之间移动文件。它运行在 TCP 之上,就像 HTTP 一样。为了传输文件,FTP 并行使用 2 个 TCP 连接:控制连接和数据连接。
为了上传和下载文件,我们将使用Python中的ftplib模块。它是Python中的内置模块。
什么是 ftplib mdoule?
这个模块定义了类 FTP 和一些相关的项目。 FTP 类实现了 FTP 协议的客户端。您可以使用它来编写Python程序来执行各种自动 FTP 作业,例如镜像其他 FTP 服务器。
我们将使用一个测试FTP服务器,它被称为DLPTEST 我们将使用以下文本文件进行所有操作:
让我们一步一步地理解实现:
- 输入必填信息,该信息将可用单击此处。
Python3
# Import Module
import ftplib
# Fill Required Information
HOSTNAME = "ftp.dlptest.com"
USERNAME = "dlpuser@dlptest.com"
PASSWORD = "eUj8GeW55SvYaswqUyDSm5v6N"
Python3
# Connect FTP Server
ftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD)
# force UTF-8 encoding
ftp_server.encoding = "utf-8"
Python3
# Enter File Name with Extension
filename = "gfg.txt"
# Read file in binary mode
with open(filename, "rb") as file:
# Command for Uploading the file "STOR filename"
ftp_server.storbinary(f"STOR {filename}", file)
Python3
# Get list of files
ftp_server.dir()
Python3
# Enter File Name with Extension
filename = "gfg.txt"
# Write file in binary mode
with open(filename, "wb") as file:
# Command for Downloading the file "RETR filename"
ftp_server.retrbinary(f"RETR {filename}", file.write)
Python3
# Display the content of downloaded file
file= open(filename, "r")
print('File Content:', file.read())
# Close the Connection
ftp_server.quit()
Python3
# Import Module
import ftplib
# Fill Required Information
HOSTNAME = "ftp.dlptest.com"
USERNAME = "dlpuser@dlptest.com"
PASSWORD = "eUj8GeW55SvYaswqUyDSm5v6N"
# Connect FTP Server
ftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD)
# force UTF-8 encoding
ftp_server.encoding = "utf-8"
# Enter File Name with Extension
filename = "File Name"
# Read file in binary mode
with open(filename, "rb") as file:
# Command for Uploading the file "STOR filename"
ftp_server.storbinary(f"STOR {filename}", file)
# Get list of files
ftp_server.dir()
# Close the Connection
ftp_server.quit()
Python3
# Import Module
import ftplib
# Fill Required Information
HOSTNAME = "ftp.dlptest.com"
USERNAME = "dlpuser@dlptest.com"
PASSWORD = "eUj8GeW55SvYaswqUyDSm5v6N"
# Connect FTP Server
ftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD)
# force UTF-8 encoding
ftp_server.encoding = "utf-8"
# Enter File Name with Extension
filename = "gfg.txt"
# Write file in binary mode
with open(filename, "wb") as file:
# Command for Downloading the file "RETR filename"
ftp_server.retrbinary(f"RETR {filename}", file.write)
# Get list of files
ftp_server.dir()
# Display the content of downloaded file
file= open(filename, "r")
print('File Content:', file.read())
# Close the Connection
ftp_server.quit()
注意:密码会不时更改,请确保您访问他们的网站以获取正确的凭据。
- 连接到服务器
蟒蛇3
# Connect FTP Server
ftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD)
# force UTF-8 encoding
ftp_server.encoding = "utf-8"
- 上传文件(要上传文件,我们将使用storbinary()方法)
句法:
# Store a file in binary transfer mode
storbinary(command, **)
蟒蛇3
# Enter File Name with Extension
filename = "gfg.txt"
# Read file in binary mode
with open(filename, "rb") as file:
# Command for Uploading the file "STOR filename"
ftp_server.storbinary(f"STOR {filename}", file)
- 使用dir()方法获取目录列表。测试服务器将在 30 分钟后删除文件。
蟒蛇3
# Get list of files
ftp_server.dir()
输出:
- 下载文件(要下载文件,我们将使用retrbinary()方法。
蟒蛇3
# Enter File Name with Extension
filename = "gfg.txt"
# Write file in binary mode
with open(filename, "wb") as file:
# Command for Downloading the file "RETR filename"
ftp_server.retrbinary(f"RETR {filename}", file.write)
- 关闭FTP连接。
蟒蛇3
# Display the content of downloaded file
file= open(filename, "r")
print('File Content:', file.read())
# Close the Connection
ftp_server.quit()
输出:
以下是FTP服务器上传文件的完整程序:
蟒蛇3
# Import Module
import ftplib
# Fill Required Information
HOSTNAME = "ftp.dlptest.com"
USERNAME = "dlpuser@dlptest.com"
PASSWORD = "eUj8GeW55SvYaswqUyDSm5v6N"
# Connect FTP Server
ftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD)
# force UTF-8 encoding
ftp_server.encoding = "utf-8"
# Enter File Name with Extension
filename = "File Name"
# Read file in binary mode
with open(filename, "rb") as file:
# Command for Uploading the file "STOR filename"
ftp_server.storbinary(f"STOR {filename}", file)
# Get list of files
ftp_server.dir()
# Close the Connection
ftp_server.quit()
输出:
下面是在 FTP 服务器中下载文件的完整程序:
蟒蛇3
# Import Module
import ftplib
# Fill Required Information
HOSTNAME = "ftp.dlptest.com"
USERNAME = "dlpuser@dlptest.com"
PASSWORD = "eUj8GeW55SvYaswqUyDSm5v6N"
# Connect FTP Server
ftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD)
# force UTF-8 encoding
ftp_server.encoding = "utf-8"
# Enter File Name with Extension
filename = "gfg.txt"
# Write file in binary mode
with open(filename, "wb") as file:
# Command for Downloading the file "RETR filename"
ftp_server.retrbinary(f"RETR {filename}", file.write)
# Get list of files
ftp_server.dir()
# Display the content of downloaded file
file= open(filename, "r")
print('File Content:', file.read())
# Close the Connection
ftp_server.quit()
输出: