Golang,在Localhost服务器中如何打印IP? (127.0.0.1)

发布于 2025-02-10 14:12:36 字数 1215 浏览 1 评论 0原文

我有此代码尝试获取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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哭泣的笑容 2025-02-17 14:12:36

我假设您正在寻找请求者的IP?

您应该能够使用remoteaddr属性来抓住它。

func (m *Repostory) HomeHandeler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "IP: %s", r.RemoteAddr)
}

或者,如果您正在寻找请求的服务器IP,则可以从请求的上下文中获取它。

func (m *Repostory) HomeHandeler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "IP: %s", r.Context().Value(http.LocalAddrContextKey))
}

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.

func (m *Repostory) HomeHandeler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "IP: %s", r.RemoteAddr)
}

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.

func (m *Repostory) HomeHandeler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "IP: %s", r.Context().Value(http.LocalAddrContextKey))
}

https://pkg.go.dev/net/http#Request

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文