Small Rust scripts
at main 48 lines 1.2 kB view raw
1#!/usr/bin/env -S cargo +nightly -Zscript 2 3--- 4[package] 5edition = "2024" 6[dependencies] 7argh = "0.1" 8--- 9 10use std::io::{BufRead, BufReader}; 11use std::net::TcpListener; 12use std::str; 13 14use argh::FromArgs; 15 16#[derive(FromArgs)] 17#[argh(description = "simple debug server which logs the data sent to it.")] 18struct App { 19 #[argh( 20 option, 21 description = "interface to listen on.", 22 default = "\"0.0.0.0\".to_string()" 23 )] 24 host: String, 25 26 #[argh(option, description = "port to listen on.", default = "7331")] 27 port: u16, 28} 29 30fn main() { 31 let app: App = argh::from_env(); 32 33 let address = format!("{}:{}", app.host, app.port); 34 let listener = TcpListener::bind(&address).expect("should be able to start server"); 35 36 println!("Listening on {}", address); 37 38 for stream in listener.incoming() { 39 let stream = stream.expect("stream should be present"); 40 let mut stream = BufReader::new(stream); 41 42 // This is not the "best" way to get the data sent over 43 // but it sure is the easiest :^) 44 let data = stream.fill_buf().expect("should have data"); 45 let output = str::from_utf8(data).expect("should be able to parse data as text"); 46 println!("{}", output); 47 } 48}