一文看明白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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值