dtrt2368 2018-08-28 14:53
浏览 37
已采纳

在不劫持连接的情况下获取本地HTTP服务器的端口

In a http handler is there a way to get the port of the server that received the request without using http.Hijacker to get the TCP connection, if there isn't is there a way to hijack the connection but still use the provided http.ResponseWriter

  • 写回答

1条回答 默认 最新

  • douzhi4830 2018-08-28 15:37
    关注

    Get the local address from the request context using http.LocalAddrContextKey.

    a, ok := req.Context().Value(http.LocalAddrContextKey).(net.Addr)
    if !ok {
        // handle address not found
    }
    

    Get the TCP port from the address:

    ta, ok := a.(*net.TCPAddr)
    if !ok {
        // handle unknown address type
    }
    port := ta.Port
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决