connecting/
connecting.rs

1extern crate ftp;
2
3use std::str;
4use std::io::Cursor;
5use ftp::{FtpStream, FtpError};
6
7fn test_ftp(addr: &str, user: &str, pass: &str) -> Result<(), FtpError> {
8    let mut ftp_stream = FtpStream::connect((addr, 21)).unwrap();
9    ftp_stream.login(user, pass).unwrap();
10    println!("current dir: {}", ftp_stream.pwd().unwrap());
11
12    ftp_stream.cwd("test_data").unwrap();
13
14    // An easy way to retrieve a file
15    let cursor = ftp_stream.simple_retr("ftpext-charter.txt").unwrap();
16    let vec = cursor.into_inner();
17    let text = str::from_utf8(&vec).unwrap();
18    println!("got data: {}", text);
19
20    // Store a file
21    let file_data = format!("Some awesome file data man!!");
22    let mut reader = Cursor::new(file_data.into_bytes());
23    ftp_stream.put("my_random_file.txt", &mut reader).unwrap();
24
25    ftp_stream.quit()
26}
27
28fn main() {
29    test_ftp("127.0.0.1", "anonymous", "rust-ftp@github.com").unwrap();
30    println!("test successful")
31}