

Python实用模块(二十七)requests
source link: https://xugaoxiang.com/2020/11/28/python-module-requests/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

软硬件环境
视频看这里
此处是youtube
的播放链接,需要科学上网。喜欢我的视频,请记得订阅我的频道,打开旁边的小铃铛,点赞并分享,感谢您的支持。
requests
是用来在Python
中进行标准HTTP
请求的第三方库。 它将请求背后的复杂性抽象成一个漂亮,简单的API
,以便你可以专注于与服务交互和在应用程序中使用数据。
使用pip
进行安装
pip install requests
http请求格式
requests
的使用其实非常简单,针对不同的http
方法,分别有不同的方法请求对应,如get
、post
、delete
等
import requests
# get请求
r = requests.get(url='url')
r = requests.post(url='url')
r = requests.put(url='url')
r = requests.delete(url='url')
r = requests.head(url='url')
r = requests.options(url='url')
利用前面我们分享的flask restful
教程,先写个后台程序
from flask import Flask, jsonify, request
from flask_restful import Api, Resource, reqparse
USERS = [
{"name": "zhangsan"},
{"name": "lisi"},
{"name": "wangwu"},
{"name": "zhaoliu"}
]
class Users(Resource):
def get(self):
return jsonify(USERS)
def post(self):
args = reqparse.RequestParser() \
.add_argument('name', type=str, location='json', required=True, help="名字不能为空") \
.parse_args()
if args['name'] not in USERS:
USERS.append({"name": args['name']})
return jsonify(USERS)
def delete(self):
USERS = []
return jsonify(USERS)
class UserId(Resource):
def __init__(self):
self.parser = reqparse.RequestParser()
self.parser.add_argument('name', type=str)
self.parser.add_argument('age', type=int)
def get(self, userid):
datas = self.parser.parse_args()
return jsonify(
{"name": USERS[int(userid)].get('name'), "age": datas.get('age')}
)
def post(self, userid):
file = request.files['file']
file.save('flask_file.txt')
return jsonify({
'msg' : 'success'
})
app = Flask(__name__)
api = Api(app, default_mediatype="application/json")
api.add_resource(Users, '/users')
api.add_resource(UserId, '/user/<userid>')
app.run(host='0.0.0.0', port=5000, use_reloader=True, debug=True)
完成后,启动flask
服务
get请求示例
先看个不带参的get
请求
import requests
r = requests.get('http://127.0.0.1:5000/users')
print(r.json())
print(r.status_code)
运行结果如下
再看个带参数的get
请求
import requests
param = {"name":"lisi", "age":"18"}
r = requests.get('http://127.0.0.1:5000/user/1', params=param)
print(r.json())
print(r.status_code)
运行结果如下
post请求示例
再来看看post
请求,携带json
数据
import requests
import json
param = {"name" : "xgx"}
headers = {"Content-type": "application/json"}
r = requests.post('http://127.0.0.1:5000/users', data=json.dumps(param), headers=headers)
print(r.json())
print(r.status_code)
运行结果如下
再来看看post
请求时提交文件的示例
import requests
files = {'file': open('test.txt', 'rb')}
r = requests.post('http://127.0.0.1:5000/user/1', files=files)
print(r.json())
print(r.status_code)
运行结果如下
delete请求示例
最后看看delete
请求示例
import requests
r = requests.delete('http://127.0.0.1:5000/users')
print(r.json())
print(r.status_code)
运行结果如下
Python实用模块专题
更多有用的python
模块,请移步
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK