Skip to content

Instantly share code, notes, and snippets.

@DrMetallius
Created August 31, 2019 20:32
Show Gist options
  • Save DrMetallius/b4831ff48ab8f4e285950362bce7916f to your computer and use it in GitHub Desktop.
Save DrMetallius/b4831ff48ab8f4e285950362bce7916f to your computer and use it in GitHub Desktop.
FTP in Rust
extern crate ftp;
use ftp::FtpStream;
use std::fs::File;
use std::error::Error;
use std::io;
fn main() {
let result = download("example.com:21", "anonymous", "anonymous", ".", "Test.txt");
match result {
Ok(bytes_written) => println!("Wrote {} bytes", bytes_written),
Err(error) => println!("Error occurred: {}", error)
}
}
fn download(addr: &str, username: &str, password: &str, dir: &str, file: &str) -> Result<u64, Box<Error>> {
let mut stream = FtpStream::connect(addr)?;
stream.login(username, password)?;
stream.cwd(dir)?;
println!("Current directory: {}", stream.pwd()?);
for file in stream.list(None)? {
println!("{}", file);
}
Ok(io::copy(&mut stream.get(file)?, &mut File::create(file)?)?)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment