一文看明白Python requests模块的get和post请求到底发的是啥?

1 前言

(郑重声明:本博文版权归扫地僧-smile所有,博文禁止转载!)

(关注博主,不定期更新博客,每一篇都是精品哦,满满干货!!!)

扫地僧-smile 潜心打造保姆级知识点博客,从提出疑问到全面解决,仅看此就够了。本博客汇聚以下优势

  • 问题相关知识齐全

  • 解决问题逻辑清晰

  • 所有演示代码均可用:无乱码,注释清晰,可复制,全部代码均自己开发,测试无误后上传。

2 GET 请求

*注意:每一个小节分为两个部分内容。上面的内容为python的 requests 请求,下面的内容为 socket 抓包获取的原始 http请求报文

  • 网址

    requests.get(url = "http://127.0.0.1:6000/")
    
    GET / HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    
  • 带参数

    requests.get(url = "http://127.0.0.1:6000/?name=smile")
    
    GET /?name=smile HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    
  • 带参数

    params = {
          
          
        "name": "smile"
    }
    
    requests.get(url="http://127.0.0.1:6000/", params = params)
    
    GET /?name=smile HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    
  • json发送

    json_ = {
          
          
        "name": "smile"
    }
    
    requests.get(url="http://127.0.0.1:6000/", json = json_ )
    
    GET / HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 17
    Content-Type: application/json
    
    {"name": "smile"}
    
  • data发送

    data = {
          
          
        "name": "smile"
    }
    
    requests.get(url="http://127.0.0.1:6000/", data = data)
    
    GET / HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 10
    Content-Type: application/x-www-form-urlencoded
    
    name=smile
    

3 POST请求

*注意:每一个小节分为两个部分内容。上面的内容为python的 requests 请求,下面的内容为 socket 抓包获取的原始 http请求报文

  • 网址

    requests.post(url = "http://127.0.0.1:6000/")
    
    POST / HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 0
    
  • 带参数

    requests.post(url = "http://127.0.0.1:6000/?name=smile")
    
    POST /?name=smile HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 0
    
  • 带参数

    params = {
          
          
        "name": "smile"
    }
    
    requests.post(url="http://127.0.0.1:6000/", params = params)
    
    POST /?name=smile HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 0
    
  • json发送

    json_ = {
          
          
        "name": "smile"
    }
    
    requests.post(url="http://127.0.0.1:6000/", json_ = data)
    
    POST / HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 17
    Content-Type: application/json
    
    {"name": "smile"}
    
  • data发送

    data = {
          
          
        "name": "smile",
        "age": "26"
    }
    
    requests.post(url="http://127.0.0.1:6000/", data = data)
    
    POST / HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 10
    Content-Type: application/x-www-form-urlencoded
    
    name=smile&age=26
    

4 谢谢大家观看

猜你喜欢

转载自blog.csdn.net/z132533/article/details/125952307