zola/components/utils/src/net.rs
2020-02-02 17:45:16 -08:00

12 lines
383 B
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use std::net::TcpListener;
pub fn get_available_port(avoid: u16) -> Option<u16> {
// Start after "well-known" ports (01023) as they require superuser
// privileges on UNIX-like operating systems.
(1024..9000).find(|port| *port != avoid && port_is_available(*port))
}
pub fn port_is_available(port: u16) -> bool {
TcpListener::bind(("127.0.0.1", port)).is_ok()
}