之前也是简单记载了一下获取http中的ip地址
这是之前的文章
https://blog.csdn.net/shunuanwei/article/details/121746761?spm=1001.2014.3001.5501
这次,我突然想到这个地方 ,我在仔细研究一下,或者说,在调整一下代码
嗯~~
import "net/http"
// 获取请求中的IP
func RemoteIp(req *http.Request) string {
var remoteAddr string
// RemoteAddr
remoteAddr = req.RemoteAddr
if remoteAddr != "" {
return remoteAddr
}
// ipv4
remoteAddr = req.Header.Get("ipv4")
if remoteAddr != "" {
return remoteAddr
}
//
remoteAddr = req.Header.Get("XForwardedFor")
if remoteAddr != "" {
return remoteAddr
}
// X-Forwarded-For
remoteAddr = req.Header.Get("X-Forwarded-For")
if remoteAddr != "" {
return remoteAddr
}
// X-Real-Ip
remoteAddr = req.Header.Get("X-Real-Ip")
if remoteAddr != "" {
return remoteAddr
} else {
remoteAddr = "127.0.0.1"
}
return remoteAddr
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)