600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Golang 获取当前外网IP/地址/运营商

Golang 获取当前外网IP/地址/运营商

时间:2023-09-19 15:46:44

相关推荐

Golang 获取当前外网IP/地址/运营商

Golang 获取当前外网IP/地址/运营商 - Go语言中文网 - Golang中文社区

package mainimport ("fmt""io/ioutil""net/http")func main() {responseClient, errClient := http.Get("/?ip") // 获取外网 IPif errClient != nil {fmt.Printf("获取外网 IP 失败,请检查网络\n")panic(errClient)}// 程序在使用完 response 后必须关闭 response 的主体。defer responseClient.Body.Close()body, _ := ioutil.ReadAll(responseClient.Body)clientIP := fmt.Sprintf("%s", string(body))print(clientIP)}

nginx配置

获取外网IP的ngnix配置:在nginx中添加:location /get_ip { default_type text/plain; return 200 "$remote_addr\n";}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。