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

# 服务端代码,可以在本地执行也可以在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)
# 用户端代码:如果服务端是在本地则地址为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

# 服务端:设置 /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
# 客户端(请求):
params = {'username':'hello_username2'}
response1 = requests.get('http://127.0.0.1:5000/get_hello2',params=params)
print(response1.text)

3、获取表单data数据

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

4、获取JSON数据

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

完整代码:

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

服务端:

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)

客户端:

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)

jflash是一种常用于嵌入式系统开发的烧录软件,用于将固件程序烧录到目标设备的闪存中。下面是一个简单的jflash使用教程: 1. 准备工作:首先,确保你已经安装了J-Link驱动程序,并将J-Link与目标设备正确连接。 2. 打开jflash:双击打开jflash应用程序。 3. 创建新工程:选择"New"按钮或者点击菜单栏中的"File",然后选择"New"来创建一个新的jflash工程。 4. 选择目标设备:在弹出的对话框中,选择你要烧录固件的目标设备类型。如果你的设备不在列表中,可以选择"Other Devices"并手动输入设备信息。 5. 配置连接:在"Target"选项卡中,选择"Settings"按钮来配置连接参数,例如调试接口和目标设备的电压等级。确保配置正确,然后点击"OK"保存设置。 6. 添加待烧录的文件:在"File"选项卡中,点击"Add"按钮来添加待烧录的文件。可以选择单个文件或者选择整个文件夹。确保选择了正确的固件文件和路径。 7. 配置烧录参数:在"Options"选项卡中,可以配置烧录相关的参数,例如擦除闪存、烧录速度等。根据需要进行个性化设置。 8. 开始烧录:在"Project"选项卡中,点击"Download"按钮来开始烧录固件。烧录过程将显示在"Output"窗口中,可以查看烧录的进度和状态。 9. 烧录完成:烧录完成后,"Output"窗口将显示成功的信息。此时,你可以重启目标设备,运行烧录好的固件。 10. 保存工程:为了方便将来的使用,你可以选择"Save Project"来保存当前的jflash工程。 以上就是一个简单的jflash使用教程,希望对你有所帮助。请注意,在操作过程中遵循相关的安全操作规程,确保不会对目标设备造成损坏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云溪·

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值