zola/components/utils/src/net.rs
Vincent Prouillet b7ce4e59fb rustfmt
2018-10-31 08:18:57 +01:00

13 lines
312 B
Rust

use std::net::TcpListener;
pub fn get_available_port(avoid: u16) -> Option<u16> {
(1000..9000).find(|port| *port != avoid && port_is_available(*port))
}
pub fn port_is_available(port: u16) -> bool {
match TcpListener::bind(("127.0.0.1", port)) {
Ok(_) => true,
Err(_) => false,
}
}