FTP(File Transfer Protocol)服务器是一种用于在网络上进行文件传输的服务。Windows Server 2003 是微软推出的一款服务器操作系统,它内置了FTP服务组件,可以用来搭建FTP服务器。
FTP是一种应用层协议,它使用TCP进行数据传输,通常运行在20和21端口上。FTP有两种工作模式:主动模式和被动模式。主动模式下,服务器主动连接客户端的某个端口来传输数据;被动模式下,服务器开启一个端口等待客户端连接。
import ftplib
def ftp_connect(host, user, passwd):
try:
ftp = ftplib.FTP(host)
ftp.login(user=user, passwd=passwd)
print("Connected to FTP server successfully.")
return ftp
except Exception as e:
print(f"Failed to connect to FTP server: {e}")
return None
def upload_file(ftp, local_path, remote_path):
try:
with open(local_path, 'rb') as file:
ftp.storbinary(f'STOR {remote_path}', file)
print(f"File {local_path} uploaded to {remote_path}.")
except Exception as e:
print(f"Failed to upload file: {e}")
# 使用示例
ftp = ftp_connect('ftp.example.com', 'username', 'password')
if ftp:
upload_file(ftp, 'local_file.txt', 'remote_file.txt')
ftp.quit()
请注意,实际使用时需要替换示例代码中的服务器地址、用户名、密码以及文件路径为实际值。
领取专属 10元无门槛券
手把手带您无忧上云