Flask学习笔记(五):基于Flask的requests.post() 方法的使用

1、方法定义

官方文档:http://docs.python-requests.org/en/master/api/
在这里插入图片描述

1.1、源码

在这里插入图片描述

1.2、常用返回信息

在这里插入图片描述

2、post方法简单使用

1、带数据的post:

# -*- coding:utf-8 -*-
import requests
import json
 
host = "http://httpbin.org/"
endpoint = "post"
url = ''.join([host,endpoint])
data = {'key1':'value1','key2':'value2'}
 
r = requests.post(url,data=data)
#response = r.json()
print (r.text)

输出:

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "key1": "value1", 
    "key2": "value2"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "23", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.1"
  }, 
  "json": null, 
  "origin": "183.14.133.88", 
  "url": "http://httpbin.org/post"
}

2、带header的post:

# -*- coding:utf-8 -*-
import requests
import json
 
host = "http://httpbin.org/"
endpoint = "post"
 
url = ''.join([host,endpoint])
headers = {"User-Agent":"test request headers"}
 
# r = requests.post(url)
r = requests.post(url,headers=headers)
#response = r.json()

输出:

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "0", 
    "Host": "httpbin.org", 
    "User-Agent": "test request headers"
  }, 
  "json": null, 
  "origin": "183.14.133.88", 
  "url": "http://httpbin.org/post"
}

3、带json的post:

# -*- coding:utf-8 -*-
import requests
import json
 
host = "http://httpbin.org/"
endpoint = "post"
 
url = ''.join([host,endpoint])
data = {
    "sites": [
                { "name":"test" , "url":"www.test.com" },
                { "name":"google" , "url":"www.google.com" },
                { "name":"weibo" , "url":"www.weibo.com" }
    ]
}
 
r = requests.post(url,json=data)
# r = requests.post(url,data=json.dumps(data))
response = r.json()

输出:

{
  "args": {}, 
  "data": "{\"sites\": [{\"url\": \"www.test.com\", \"name\": \"test\"}, {\"url\": \"www.google.com\", \"name\": \"google\"}, {\"url\": \"www.weibo.com\", \"name\": \"weibo\"}]}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "140", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.1"
  }, 
  "json": {
    "sites": [
      {
        "name": "test", 
        "url": "www.test.com"
      }, 
      {
        "name": "google", 
        "url": "www.google.com"
      }, 
      {
        "name": "weibo", 
        "url": "www.weibo.com"
      }
    ]
  }, 
  "origin": "183.14.133.88", 
  "url": "http://httpbin.org/post"
}

4、带参数的post:

# -*- coding:utf-8 -*-
import requests
import json
 
host = "http://httpbin.org/"
endpoint = "post"
 
url = ''.join([host,endpoint])
params = {'key1':'params1','key2':'params2'}
 
# r = requests.post(url)
r = requests.post(url,params=params)
#response = r.json()
print (r.text)

输出:

{
  "args": {
    "key1": "params1", 
    "key2": "params2"
  }, 
  "data": "", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "0", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.1"
  }, 
  "json": null, 
  "origin": "183.14.133.88", 
  "url": "http://httpbin.org/post?key2=params2&key1=params1"
}

5、普通文件上传:

# -*- coding:utf-8 -*-
import requests
import json
 
host = "http://httpbin.org/"
endpoint = "post"
 
url = ''.join([host,endpoint])
#普通上传
files = {
            'file':open('test.txt','rb')
        }
 
r = requests.post(url,files=files)
print (r.text)

输出:

{
  "args": {}, 
  "data": "", 
  "files": {
    "file": "hello world!\n"
  }, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "157", 
    "Content-Type": "multipart/form-data; boundary=392865f79bf6431f8a53c9d56c62571e", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.1"
  }, 
  "json": null, 
  "origin": "183.14.133.88", 
  "url": "http://httpbin.org/post"
}

6、定制化文件上传:

# -*- coding:utf-8 -*-
import requests
import json
 
host = "http://httpbin.org/"
endpoint = "post"
 
url = ''.join([host,endpoint])
#自定义文件名,文件类型、请求头
files = {
        'file':('test.png',open('test.png','rb'),'image/png')
}
 
r = requests.post(url,files=files)
print (r.text)heman793

7、多文件上传:

# -*- coding:utf-8 -*-
import requests
import json
 
host = "http://httpbin.org/"
endpoint = "post"
 
url = ''.join([host,endpoint])
#多文件上传
files = [
    ('file1',('test.txt',open('test.txt', 'rb'))),
    ('file2', ('test.png', open('test.png', 'rb')))
    ]
 
r = requests.post(url,files=files)
print (r.text)

8、流式上传:

# -*- coding:utf-8 -*-
import requests
import json
 
host = "http://httpbin.org/"
endpoint = "post"
 
url = ''.join([host,endpoint])
 
#流式上传
with open( 'test.txt' ) as f:
    r = requests.post(url,data = f)
 
print (r.text)

输出:

{
  "args": {}, 
  "data": "hello world!\n", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "13", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.1"
  }, 
  "json": null, 
  "origin": "183.14.133.88", 
  "url": "http://httpbin.org/post"
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值