Golang,在Localhost服务器中如何打印IP? (127.0.0.1)
我有此代码尝试获取Localhost IP 127.0.0.1
func (m *Repostory) HomeHandeler(w http.ResponseWriter, r *http.Request) {
// Loop over header names
for name, values := range r.Header {
// Loop over all values for the name.
for _, value := range values {
fmt.Println(name, value)
}
}
remoteIP := utils.ReadUserIP(r)
}
循环的结果:
Sec-Ch-Ua " Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"
Sec-Ch-Ua-Mobile ?0
Cookie csrf_token=4f7iGnCzBTPXwP6snm6Gxxxc3Qe6P9GwVcaR7F3vUtmRuE=; session=yA0nwu4wWMH up79U9ERfzuWg
Accept-Language en,en-US;q=0.9,he;q=0.8
Connection keep-alive
Cache-Control max-age=0
Sec-Ch-Ua-Platform "Windows"
Upgrade-Insecure-Requests 1
User-Agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36
Accept text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Mode navigate
Sec-Fetch-User ?1
Accept-Encoding gzip, deflate, br
Sec-Fetch-Site none
Sec-Fetch-Dest document
但是Remoteip的结果:
[::1]:55310
i have this code where i try to get the localhost ip 127.0.0.1
func (m *Repostory) HomeHandeler(w http.ResponseWriter, r *http.Request) {
// Loop over header names
for name, values := range r.Header {
// Loop over all values for the name.
for _, value := range values {
fmt.Println(name, value)
}
}
remoteIP := utils.ReadUserIP(r)
}
the result of the loop :
Sec-Ch-Ua " Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"
Sec-Ch-Ua-Mobile ?0
Cookie csrf_token=4f7iGnCzBTPXwP6snm6Gxxxc3Qe6P9GwVcaR7F3vUtmRuE=; session=yA0nwu4wWMH up79U9ERfzuWg
Accept-Language en,en-US;q=0.9,he;q=0.8
Connection keep-alive
Cache-Control max-age=0
Sec-Ch-Ua-Platform "Windows"
Upgrade-Insecure-Requests 1
User-Agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36
Accept text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Mode navigate
Sec-Fetch-User ?1
Accept-Encoding gzip, deflate, br
Sec-Fetch-Site none
Sec-Fetch-Dest document
but the result of remoteIP :
[::1]:55310
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您正在寻找请求者的IP?
您应该能够使用
remoteaddr
属性来抓住它。或者,如果您正在寻找请求的服务器IP,则可以从请求的上下文中获取它。
https://pkg.go.do.dev/net/httppp#request
I'm assuming you're looking for the requester's IP?
You should be able to grab it using the
RemoteAddr
attribute.Or if you're looking for the the servers IP that the request came in on, you can get it from the request's context.
https://pkg.go.dev/net/http#Request