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

1 篇文章 0 订阅
1 篇文章 0 订阅

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 谢谢大家观看

  • 2
    点赞
  • 1
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:游动-白 设计师:我叫白小胖 返回首页
评论

打赏作者

扫地僧-smile

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值