Go net/http客户端和服务端抓包分析

简介

net/http 包为Golang官方的http包,可以用于实现http客户端和服务端;虽然实际项目过程中用resty更加的方便,但是通过对net/http的使用和抓包分析,可以更好的理解HTTP协议实现的原理。

示例

服务端

  • 监听8090端口。
  • 实现/hello接口。
golang 复制代码
package main

import (
    "fmt"
    "net/http"
)

func hello(w http.ResponseWriter, req *http.Request) {
    fmt.Fprintf(w, "hello\n")
}

func main() {
    http.HandleFunc("/hello", hello)
    http.ListenAndServe(":8090", nil)
}

客户端

  • 连续进行三次HTTP请求。
  • 打印HTTP响应状态码和Body。
golang 复制代码
package main

import (
    "fmt"
	"time"
	"io"
    "net/http"
)

func hello() {
	resp, err := http.Get("http://127.0.0.1:8090/hello")
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    fmt.Println("Response status:", resp.Status)

    bs, err := io.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("http get err : ", err)
        return
    }
 
    fmt.Println("body: ", string(bs))
}

func main() {
    for i:=0; i < 3; i++ {
		go func() {
			hello()
		}()
	}
	time.Sleep(600*time.Second)
}

抓包分析

tcpdump -i lo port 8090 -envv -w http.pcap

  • 在客户端连续发起三次HTTP请求后,netstat 命令可以查看到有两个连接是ESTABLISHED一个连接是TIME_WAIT,在等待足够的时间后恢复到初始状态,即连接都被释放。
bash 复制代码
[xiaofeng@localhost gostudy]$ netstat -antu|grep 8090
tcp6       0      0 :::8090                 :::*                    LISTEN     
[xiaofeng@localhost gostudy]$ 
[xiaofeng@localhost gostudy]$ 
[xiaofeng@localhost gostudy]$ netstat -antu|grep 8090
tcp        0      0 127.0.0.1:48836         127.0.0.1:8090          ESTABLISHED
tcp        0      0 127.0.0.1:48834         127.0.0.1:8090          ESTABLISHED
tcp        0      0 127.0.0.1:48838         127.0.0.1:8090          TIME_WAIT  
tcp6       0      0 :::8090                 :::*                    LISTEN     
tcp6       0      0 127.0.0.1:8090          127.0.0.1:48834         ESTABLISHED
tcp6       0      0 127.0.0.1:8090          127.0.0.1:48836         ESTABLISHED
[xiaofeng@localhost gostudy]$ netstat -antu|grep 8090
tcp6       0      0 :::8090                 :::*                    LISTEN 
  • 抓包分析
  1. wireshark追踪第一条HTTP流如下,可以推断出为ESTABLISHED中源端口48834的连接。
    • TCP三次握手建立连接
    • HTTP GET请求
    • HTTP响应hello
    • Keep-Alive保持连接
    • TCP四次挥手断开连接
  2. wireshark追踪第二条HTTP流如下,可以推断出为ESTABLISHED中源端口48836的连接。
    • TCP三次握手建立连接
    • HTTP GET请求
    • HTTP响应hello
    • Keep-Alive保持连接
    • TCP四次挥手断开连接
  3. wireshark追踪第三条HTTP流如下,可以推断出为TIME_WAIT中源端口48838的连接。
    • TCP三次握手建立连接
    • HTTP GET请求
    • HTTP响应hello
    • TCP四次挥手断开连接

总结

  1. 可以通过示例代码了解net/http最基本的用法。
  2. 可以通过抓包分析了解http协议交互过程和协议字段。
  3. 可以通过Keep-Alive了解到每个主机默认最大空闲连接数为2(DefaultMaxIdleConnsPerHost)。
相关推荐
涡能增压发动积7 小时前
同样的代码循环 10次正常 循环 100次就抛异常?自定义 Comparator 的 bug 让我丢尽颜面
后端
Wenweno0o7 小时前
0基础Go语言Eino框架智能体实战-chatModel
开发语言·后端·golang
swg3213217 小时前
Spring Boot 3.X Oauth2 认证服务与资源服务
java·spring boot·后端
tyung7 小时前
一个 main.go 搞定协作白板:你画一笔,全世界都看见
后端·go
gelald7 小时前
SpringBoot - 自动配置原理
java·spring boot·后端
殷紫川8 小时前
深入拆解 Java 内存模型:从原子性、可见性到有序性,彻底搞懂 happen-before 规则
java·后端
元宝骑士8 小时前
FIND_IN_SET使用指南:场景、优缺点与MySQL优化策略
后端·mysql
用户31952370347718 小时前
记一次 PostgreSQL WAL 日志撑爆磁盘的排查
后端
nghxni8 小时前
LightESB PlatformHttp v3.0.0:JSONPath 订单转换 HTTP 路由实战
后端
武子康9 小时前
大数据-263 实时数仓-Canal 增量订阅与消费原理:MySQL Binlog 数据同步实践
大数据·hadoop·后端