日期 : 2022-09-22 10:37:49作者 : 熊人
代码如下:
func check_url(url string) {
client := &http.Client{
Timeout: time.Second * 5,
}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36")
resp, err := client.Do(req)
if err == nil {
defer resp.Body.Close()
status := resp.Status
if strings.Contains(status, "200") {
fmt.Println(url,"===请求成功===")
}
}
}
小弟刚学 go ,我需要同时并发几万个 url 请求,调用 check_url 函数,发现有时候明明可以访问的端口,却报 context deadline exceeded (Client.Timeout exceeded while awaiting headers) 错误,怎么优化这个 check_url 函数呢?
需要对内网进行扫描,单个请求的时候如 8888 ,可以正常请求到判断为 200 状态,当对这个 ip 并发全端口进行扫描的时候,通过打印 resp, err := client.Do(req)的 err 可知:
Get "...192.168.100.126:8888": context deadline exceeded (Client.Timeout exceeded while awaiting headers)