前往测试网站状态(ping)

huangapple go评论80阅读模式
英文:

Go to test website status (ping)

问题

有没有其他更好的方法来ping网站并检查网站是否可用?

我只需要获取状态码,而不是下载整个网站...

func Ping(domain string) int {
    timeout := time.Duration(2 * time.Second)
    dialTimeout := func(network, addr string) (net.Conn, error) {
        return net.DialTimeout(network, addr, timeout)
    }
    transport := http.Transport{
        Dial: dialTimeout,
    }
    client := http.Client{
        Transport: &transport,
    }
    url := "http://" + domain
    req, _ := http.NewRequest("GET", url, nil)
    resp, _ := client.Do(req)
    return resp.StatusCode
}

这个函数太慢了,当我使用goroutines运行时,它超过了限制并给出了错误...

谢谢!

英文:

Is there any other better way to ping websites and check if the website is available or not?

I just need to get the status code not get(download) all websites...

func Ping(domain string) int {
    timeout := time.Duration(2 * time.Second)
    dialTimeout := func(network, addr string) (net.Conn, error) {
	    return net.DialTimeout(network, addr, timeout)
    }
    transport := http.Transport{
	    Dial: dialTimeout,
    }
    client := http.Client{
	    Transport: &transport,
    }
    url := "http://" + domain
    req, _ := http.NewRequest("GET", url, nil)
    resp, _ := client.Do(req)
    return resp.StatusCode
}

This function is too slow and when I run with goroutines, it goes over the limits and gives me the errors...

Thanks!

答案1

得分: 5

  • 使用单个传输器。因为传输器维护了一个连接池,所以不应该随意创建和忽略传输器。
  • 根据 net/http 文档 开头的描述关闭响应体。
  • 如果只对状态感兴趣,请使用 HEAD 方法。
  • 检查错误。

代码:

var client = http.Client{
  Transport: &http.Transport{
    Dial: net.Dialer{Timeout: 2 * time.Second}.Dial,
  },
}

func Ping(domain string) (int, error) {
    url := "http://" + domain
    req, err := http.NewRequest("HEAD", url, nil)
    if err != nil {
       return 0, err
    }
    resp, err := client.Do(req)
    if err != nil {
       return 0, err
    }
    resp.Body.Close()
    return resp.StatusCode, nil
}
英文:
  • Use a single transport. Because the transport maintains a pool of connections, you should not create and ignore transports willy nilly.
  • Close the response body as described at the beginning of the net/http doc.
  • Use HEAD if you are only interested in the status.
  • Check errors.

Code:

var client = http.Client{
  Transport: &http.Transport{
	Dial: net.Dialer{Timeout: 2 * time.Second}.Dial,
  },
}

func Ping(domain string) (int, error) {
    url := "http://" + domain
    req, err := http.NewRequest("HEAD", url, nil)
    if err != nil {
       return 0, err
    }
    resp, err := client.Do(req)
    if err != nil {
       return 0, err
    }
    resp.Body.Close()
    return resp.StatusCode, nil
}

答案2

得分: 1

由于这是在Google上关于在Go中进行Ping的顶级结果,所以请知道已经有几个为此目的编写的包,但如果您计划使用这个答案,我必须进行一些更改使其正常工作。

import (
   "time"
   "net/http"
)

var client = http.Client{
   Timeout: 2 * time.Second,
}

// 其他部分与接受的答案保持一致。

但我是Go的初学者可能有更好的方法来做到这一点

<details>
<summary>英文:</summary>

Since this is the top result on Google for Pinging in Go, just know there have been several packages written for this purpose, but if you plan to use this answer, I had to make some changes for this to work.

    import (
       &quot;time&quot;
       &quot;net/http&quot;
    )

    var client = http.Client{
       Timeout: 2 * time.Second,
    }

But otherwise keeping the same with the accepted answer. 

But I&#39;m a beginner in Go so there may be a better way to do this.

</details>



huangapple
  • 本文由 发表于 2014年10月26日 05:40:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/26567529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定