调用net.LookupIP("server.example.com")导致错误:lookup server.example.com on [::1]:53: read udp [::1]:39828->[::1]:53: read: connection refused
net.LookupIP("server.example.com")
lookup server.example.com on [::1]:53: read udp [::1]:39828->[::1]:53: read: connection refused
ips, err := net.LookupIP(host)
但是nslookup server.example.com在同一台服务器上运行良好。有什么可以调试的吗?
nslookup server.example.com
x8diyxa71#
暂时,您可以使用net.Resolver结构在Go程序中指定不同的DNS服务器。以下是使用Google DNS服务器(8.8.8.8)的示例:
func main() { r := &net.Resolver{ PreferGo: true, Dial: func(ctx context.Context, network, address string) (net.Conn, error) { d := net.Dialer{} return d.DialContext(ctx, "udp", "8.8.8.8:53") }, } ips, err := r.LookupIPAddr(context.TODO(), "server.example.com") if err != nil { fmt.Println("Error:", err) return } for _, ip := range ips { fmt.Println("IP address:", ip.IP.String()) }
并且DNS查询可以通过TCP或UDP执行。在上面的例子中,我们在DialContext方法中使用了“udp”。您可以尝试使用“tcp”来查看是否可以解决问题。
1条答案
按热度按时间x8diyxa71#
暂时,您可以使用net.Resolver结构在Go程序中指定不同的DNS服务器。以下是使用Google DNS服务器(8.8.8.8)的示例:
并且DNS查询可以通过TCP或UDP执行。在上面的例子中,我们在DialContext方法中使用了“udp”。您可以尝试使用“tcp”来查看是否可以解决问题。