1 个稳定版本

使用旧版Rust 2015

3.0.1 2022年3月30日

#6#ftp-client

37 每月下载量
2 个crate中使用 (通过 rs_transfer)

Apache-2.0/MIT

35KB
597 代码行

rust-ftp

Rust FTP客户端

Number of Crate Downloads Crate Version Crate License Build Status Coverage Status

文档

安装

默认情况下禁用FTPS支持。要启用它,应在 Cargo.toml 中激活 secure

[dependencies]
ftp = { version = "<version>", features = ["secure"] }

用法

extern crate ftp;

use std::str;
use std::io::Cursor;
use ftp::FtpStream;

fn main() {
    // Create a connection to an FTP server and authenticate to it.
    let mut ftp_stream = FtpStream::connect("127.0.0.1:21").unwrap();
    let _ = ftp_stream.login("username", "password").unwrap();

    // Get the current directory that the client will be reading from and writing to.
    println!("Current directory: {}", ftp_stream.pwd().unwrap());
    
    // Change into a new directory, relative to the one we are currently in.
    let _ = ftp_stream.cwd("test_data").unwrap();

    // Retrieve (GET) a file from the FTP server in the current working directory.
    let remote_file = ftp_stream.simple_retr("ftpext-charter.txt").unwrap();
    println!("Read file with contents\n{}\n", str::from_utf8(&remote_file.into_inner()).unwrap());

    // Store (PUT) a file from the client to the current working directory of the server.
    let mut reader = Cursor::new("Hello from the Rust \"ftp\" crate!".as_bytes());
    let _ = ftp_stream.put("greeting.txt", &mut reader);
    println!("Successfully wrote greeting.txt");

    // Terminate the connection to the server.
    let _ = ftp_stream.quit();
}

许可证

许可协议为以下之一:

由您选择。

贡献

除非您明确说明,否则根据Apache-2.0许可证定义,您提交的任何旨在包含在作品中的贡献将根据上述协议双重许可,不附加任何额外条款或条件。

开发环境

您只需Rust和Docker即可开发和运行rust-ftp测试。测试文件夹包含一个 Dockerfile,用于安装和配置vsftpd服务器。

要创建Docker镜像

docker build -t ftp-server tests

要启动测试的FTP服务器

tests/ftp-server.sh

此脚本以分离模式运行 ftp-server 镜像,并启动 vsftpd 守护进程。它绑定端口21(FTP)以及被动连接的65000-65010范围。

一旦运行了一个实例,要运行测试,请输入

cargo test

以下命令可能有用

# List running containers of ftp-server image
# (to include stopped containers use -a option)
docker ps --filter ancestor=ftp-server

# To stop and remove a container
docker stop container_name
docker rm container_name

# To remove the image
docker rmi ftp-server

依赖项

~3–5MB
~83K SLoC