我用 Go 写了一个 URL 监控程序,但是一段时间后我发现在netstat -nao|grep 80.
getHttpStatusCode 函数:
HttpClient = &http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
deadline := time.Now().Add(30 * time.Second)
c, err := net.DialTimeout(netw, addr, 20*time.Second)
if err != nil {
return nil, err
}
c.SetDeadline(deadline)
c.SetReadDeadline(deadline)
c.SetWriteDeadline(deadline)
return c, nil
},
},
}
// ...
func getHttpStatusCode(url string) int {
if url == "" {
return 200
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return 0
}
req.Close = true
req.Header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17")
resp, err := HttpClient.Do(req)
if err != nil {
return 0
}
defer resp.Body.Close()
return resp.StatusCode
}
我检查了 Go 手册,没有找到像 req.Close() 这样的东西,只是推迟了 resp.Body.Close()。
这是输出netstat -nao | grep 80:
tcp 1343352 0 192.168.2.33:29581 220.181.155.19:80 建立关闭 (0.00/0/0)
和输出tcpdump tcp port 80:
14:32:54.085095 IP 113.12.80.13.http > wk_0_mysql.KIDC90805.zw.39174: Flags [.], seq 17376:18824, ack 1, win 42, options [nop,nop,TS val 4236145017 ecr 204896351], length 1448
14:32:54.109206 IP wk_0_mysql.KIDC90805.zw.25834 > 220.181.90.8.http: Flags [S], seq 714805337, win 14600, options [mss 1460,sackOK,TS val 204896416 ecr 0,nop,wscale 9], length 0
14:32:54.223349 IP 220.181.155.22.http > wk_0_mysql.KIDC90805.zw.19262: Flags [.], seq 864939135:864940583, ack 1630899997, win 42, options [nop,nop,TS val 1570834172 ecr 204896529], length 1448
喵喔喔
相关分类