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)。
相关推荐
观望过往1 小时前
Spring Boot 集成 EMQ X 4.0 完整技术指南
java·spring boot·后端·emqx
心之语歌1 小时前
对于 时间复杂度和空间复杂度分析
后端
青旬1 小时前
AI编程祛魅-最近几个失败的ai编程经历
后端·程序员
莹Innsane1 小时前
记一次 float64 排序失效的灵异事件
后端
Python私教1 小时前
使用 SQLAlchemy 操作单表:以 SQLite 用户表为例的完整实战指南
后端
Python私教2 小时前
使用 SQLAlchemy 连接数据库:从基础到最佳实践
后端
码起来呗3 小时前
基于Spring Boot的乡村拼车小程序的设计与实现-项目分享
spring boot·后端·小程序
我命由我123453 小时前
Java 并发编程 - Delay(Delayed 概述、Delayed 实现、Delayed 使用、Delay 缓存实现、Delayed 延迟获取数据实现)
java·开发语言·后端·缓存·java-ee·intellij-idea·intellij idea
我是天龙_绍4 小时前
java 比对两对象大小 重写 comparator
后端
IT_陈寒4 小时前
Python 3.12新特性实测:10个让你的代码提速30%的隐藏技巧 🚀
前端·人工智能·后端