Flask的用法

Flask一般分为两部分,一个是服务端,一个是客户端(请求方)。

一、服务端一般需要设置API接口、获取请求头、获取请求的参数(params、data、json)等等

二、客户端一般用于请求服务端的接口,配置请求头以及配置请求的参数

参数部分主要用到这几个函数:

复制代码
request.args.get('id')              # 从url参数截取
request.form.get('username')        # 从表单数据截取
request.json.get('username')        # 获取json参数数据

下面将展示Flask各个参数及配置的用法

1、基础接口

在本地启动端口为5000,名为 get_hello 的接口,同时设置请求方式,Get或POST。

在后续访问时请求 :http://127.0.0.1:5000/get_hello

python 复制代码
# 服务端代码,可以在本地执行也可以在Linux上执行
from flask import Flask,request
app = Flask(__name__)

# 在本地启动名为 get_hello 的接口,同时设置请求方式
@app.route('/get_hello', methods=['GET','POST'])
def hello():
    return "Hello, World!"

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5000)
python 复制代码
# 用户端代码:如果服务端是在本地则地址为http://127.0.0.1:5000/get_hello1
# 如果不是在本地则访问地址为 http://服务器地址:服务器开放的端口/get_hello1
import requests

response1 = requests.get('http://127.0.0.1:5000/get_hello')
print(response1.text)

2、获取URL携带的参数 params

设置 /get_hello2 的接口,并设置请求方式 GET/POST

同时获取URL携带的参数 params指定数据,

代码用法同案例1

python 复制代码
# 服务端:设置 /get_hello2 的接口,并设置请求方式 GET/POST 
# 获取URL中名为 username 的参数数据
@app.route('/get_hello2', methods=['GET','POST'])
def get_url_args():
    username = request.args.get('username')        # 获取url的参数数据
    return username
python 复制代码
# 客户端(请求):
params = {'username':'hello_username2'}
response1 = requests.get('http://127.0.0.1:5000/get_hello2',params=params)
print(response1.text)

3、获取表单data数据

python 复制代码
# 客户端:
@app.route('/get_hello3', methods=['GET','POST'])
def get_form_args():
    username = request.form.get('username')        # 获取表单data的参数数据
    return username
python 复制代码
# 服务端
data = {'username':'hello_username3'}
response1 = requests.get('http://127.0.0.1:5000/get_hello3',data=data)
print(response1.text)

4、获取JSON数据

python 复制代码
# 服务端:
@app.route('/get_hello4', methods=['GET','POST'])      # 设置请求头
def get_json():
    username = request.json.get('username')        # 获取json参数数据
    return username
python 复制代码
# 客户端:
json1 = {'username':'hello_username4'}
response1 = requests.get('http://127.0.0.1:5000/get_hello4',json=json1)
print(response1.text)

完整代码:

注意!需要先开启服务端后才能运行客户端来请求哦~

服务端:

python 复制代码
from flask import Flask,request
app = Flask(__name__)

'''
request.args.get('id')      # 从url参数截取
request.form.get('username')    # 从表单数据截取
request.json.get('username')        # 获取json参数数据
'''

# 在本地启动名为 get_hello 的接口
@app.route('/get_hello')
def hello():
    return "Hello, World!"

@app.route('/get_hello1', methods=['GET','POST'])      # 设置请求头
def hello1():
    return "Hello, World!"

@app.route('/get_hello2', methods=['GET','POST'])
def get_url_args():
    username = request.args.get('username')        # 获取url的参数数据
    return username

@app.route('/get_hello3', methods=['GET','POST'])
def get_form_args():
    username = request.form.get('username')        # 获取表单data的参数数据
    return username

@app.route('/get_hello4', methods=['GET','POST'])      # 设置请求头
def get_json():
    username = request.json.get('username')        # 获取json参数数据
    return username

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5000)

客户端:

python 复制代码
import requests

response1 = requests.get('http://127.0.0.1:5000/get_hello1')
print(response1.text)
# ============================

params = {'username':'hello_username2'}
response1 = requests.get('http://127.0.0.1:5000/get_hello2',params=params)
print(response1.text)

# ============================

data = {'username':'hello_username3'}
response1 = requests.get('http://127.0.0.1:5000/get_hello3',data=data)
print(response1.text)
# ============================

json1 = {'username':'hello_username4'}
response1 = requests.get('http://127.0.0.1:5000/get_hello4',json=json1)
print(response1.text)
相关推荐
漫谈网络1 分钟前
回调函数应用示例
开发语言·python·回调函数
亚林瓜子17 分钟前
pyenv简单的Python版本管理器(macOS版)
开发语言·python·macos·pyenv
青钰未央19 分钟前
14、Python时间表示:Unix时间戳、毫秒微秒精度与time模块实战
python·改行学it
编程乐学(Arfan开发工程师)35 分钟前
06、基础入门-SpringBoot-依赖管理特性
android·spring boot·后端
编程乐学(Arfan开发工程师)36 分钟前
05、基础入门-SpringBoot-HelloWorld
java·spring boot·后端
橘子海全栈攻城狮1 小时前
【源码+文档+调试讲解】党员之家服务系统小程序1
java·开发语言·spring boot·后端·小程序·旅游
冼紫菜1 小时前
Java开发中使用 RabbitMQ 入门到进阶详解(含注解方式、JSON配置)
java·spring boot·后端·rabbitmq·springcloud
boring_1112 小时前
Apache Pulsar 消息、流、存储的融合
分布式·后端
墨绿色的摆渡人2 小时前
pytorch小记(二十二):全面解读 PyTorch 的 `torch.cumprod`——累积乘积详解与实战示例
人工智能·pytorch·python
小白—人工智能2 小时前
数据分析 —— 数据预处理
python·数据挖掘·数据分析