当前位置:编程学习 > C/C++ >>

Golang lsof server

01 package main
02 
03 import (
04   "net/http"
05   "fmt"
06   "os/exec"
07   "flag"
08 )
09 
10 func main(){
11    http.HandleFunc("/",  readHandle)
12    port := flag.Int("port", 2012, "Listen Port")
13    flag.Parse()
14    http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
15  
16 }
17 
18 
19 func readHandle(w http.ResponseWriter, r *http.Request){
20         /* Command */
21         lsof := exec.Command("lsof", r.FormValue("path"))
22         wc := exec.Command("wc", "-l")
23         /* Pipe */
24         lsofOut,_ := lsof.StdoutPipe()
25         /* Command Start */
26         lsof.Start()
27         /* Stdin */
28         wc.Stdin = lsofOut
29         out,_ := wc.Output()
30 
31         fmt.Fprintf(w, "%v", r.FormValue("path"))
32         fmt.Fprintf(w, "%s", out)
33         fmt.Fprintf(w, "%s", r.URL.Path[1:])
34 }

 

作者:ioser
补充:软件开发 , C语言 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,