Github地址
源碼地址:https://github.com/anonymousGiga
說(shuō)明
本示例使用Rust編寫一個(gè)FTP的客戶端,在客戶端中進(jìn)行下載和上傳的演示化戳。
客戶端
- 在Cargo.toml文件中添加:
[dependencies]
ftp = "3.0.1"
- 編寫src/main.rs如下:
use std::str;
use std::io::Cursor;
use ftp::FtpStream;
fn main() {
let mut ftp_stream = FtpStream::connect("127.0.0.1:21").unwrap();
let _ = ftp_stream.login("andy1", "1").unwrap();
println!("Current directory: {}", ftp_stream.pwd().unwrap());
let _ = ftp_stream.cwd("upload").unwrap();
let remote_file = ftp_stream.simple_retr("./test").unwrap();
println!("Read file with contents\n{}\n", str::from_utf8(&remote_file.into_inner()).unwrap());
let mut reader = Cursor::new("Hello from the Rust \"ftp\" crate!".as_bytes());
let _ = ftp_stream.put("hello", &mut reader);
println!("Successfully wrote hello");
let _ = ftp_stream.quit();
}
測(cè)試
按照上一節(jié)《015 Rust網(wǎng)絡(luò)編程,F(xiàn)TP介紹》中搭建ftp server轨淌,并且創(chuàng)建用戶andy1迂烁,同時(shí)在ftp_server/andy1目錄下創(chuàng)建upload文件夾,在文件夾放置一個(gè)test文件递鹉。
在當(dāng)前工程目錄下放置一個(gè)hello文件盟步。
運(yùn)行程序:
cargo run
在ftp_server/andy1/upload下會(huì)發(fā)現(xiàn)多了hello文件,而在終端中則會(huì)打印ftp_server/andy1/upload/test文件的內(nèi)容躏结。