Flask学习笔记(三):基于Flask框架上传特征值(相关数据)到服务器端并保存为txt文件

简介: 这篇博客文章是关于如何使用Flask框架上传特征值数据到服务器端,并将其保存为txt文件的教程。

关键模块

b64decode

import base64
base64.b64decode(s, altchars=None, validate=False)

参数

  • s:要被解码的对象
  • altchars:必须是长度为2的bytes-like类型对象或ASCII字符串,它指定“+”和“/”字符的替代字母表。

返回的指为字节或者bytes

b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x02\x01\x01\x01\x01\x01\x02\x01\x0

np.fromstring

将字符串或者字节转换成ascll码

import numpy as np
np.fromstring(str/bytes, dtype=np.uint8)

客户端

import requests
import json

# API地址
url = "http://10.16.55.26:8000/feature"
feature =[-130.875, -51.84375, 63.53125, -9.265625, 9.59375, -113.3125, -127.625, -20.5, 103.4375]
id = 2
time='123'
cameraid='camera4'
conf = 0.95
# 拼接参数
files = {'file': feature,'id': id,'time':time,'cameraid':str(cameraid),'conf':str(conf)}
fff=json.dumps(files, ensure_ascii=False)
# 发送post请求到服务器端json.dumps(files, ensure_ascii=False)
# # r = requests.post(url, json=json.dumps(files, ensure_ascii=False))
r = requests.post(url, json=fff)
print('200为成功 500为失败',r.status_code)
print(type(json.dumps(files, ensure_ascii=False)))

服务器端

from flask import request
from flask import Flask
import json,os
import numpy as np
import pickle
import cv2
import base64
from gevent.pywsgi import WSGIServer

app = Flask(__name__)

# 定义路由 flask的路由函数是一定要有返回值的,在不执行if的时候也需要加上返回值
@app.route("/feature", methods=['POST'])
def get_frame():
    filename=request.get_json()
    if (isinstance(filename,str)):
        filname1=json.loads(filename)
        print(filname1)
        feature_data=filname1['file']
        id=filname1['id']
        time=filname1['time']
        cameraid=filname1['cameraid']
        conf=filname1['conf']
        if cameraid=='camera1':
            f = open('F:\pycharm\client\camera1.txt','w') # a只写到最后,不可读 a+两者都可以
            f.write(str(feature_data)+'/'+str(id)+'/'+str(time)+'/'+str(cameraid)+'/'+str(conf)+'\n')
        elif cameraid=='camera2':
            f = open('F:\pycharm\client\camera2.txt','w') # a只写到最后,不可读 a+两者都可以
            f.write(str(feature_data)+'/'+str(id)+'/'+str(time)+'/'+str(cameraid)+'/'+str(conf)+'\n')
        elif cameraid=='camera3':
            f = open('F:\pycharm\client\camera3.txt','w') # a只写到最后,不可读 a+两者都可以
            f.write(str(feature_data)+'/'+str(id)+'/'+str(time)+'/'+str(cameraid)+'/'+str(conf)+'\n')
        else:
            f = open('F:\pycharm\client\camera4.txt','w') # a只写到最后,不可读 a+两者都可以
            f.write(str(feature_data)+'/'+str(id)+'/'+str(time)+'/'+str(cameraid)+'/'+str(conf)+'\n')
    else:
        print('the params is not str')
    # linedata2=open('F:\pycharm\client\camera2.txt','r')
    # for line in linedata2:
    #     for i in line.split('/'):
    #         print(i)
    print('cameraid is :',cameraid)
    json_info = json.dumps(dic, ensure_ascii=False) #将python对象编码成Json字符串 ensure_ascii=True:默认输出ASCLL码,如果把这个该成False,就可以输出中文。
    return json_info

if __name__ == "__main__":
    dic = {}
    app.run(host='10.16.55.26',port=8000)

效果如下
在这里插入图片描述
在这里插入图片描述

目录
相关文章
|
1天前
|
Python
Flask学习笔记(二):基于Flask框架上传图片到服务器端并原名保存
关于如何使用Flask框架上传图片到服务器端并以其原名保存的教程。
7 1
|
3天前
|
JSON 测试技术 数据库
Python的Flask框架
【10月更文挑战第4天】Python的Flask框架
|
3天前
|
存储 安全 数据库
Flask框架中,如何实现用户身份验证和会话管理?
【10月更文挑战第4天】Flask框架中,如何实现用户身份验证和会话管理?
|
弹性计算 运维 安全
阿里云云服务器ECS介绍 学习笔记
阿里云云服务器ECS介绍 学习笔记
172 0
|
1天前
|
存储 机器学习/深度学习 应用服务中间件
阿里云倚天云服务器实例:计算型c8y、通用型g8y、内存型r8y实例介绍
阿里云倚天云服务器是基于阿里云自研的倚天710 ARM架构CPU打造的高性能计算产品系列,它依托先进的第四代神龙架构,旨在为用户提供稳定可预期的超高效能体验。倚天云服务器在存储、网络性能及计算稳定性方面实现了显著提升,主要得益于其芯片级的快速路径加速技术。本文将深度解析阿里云倚天云服务器的计算型c8y、通用型g8y、内存型r8y实例,探讨其优势及适用场景,以供选择参考。
|
2天前
|
网络协议 应用服务中间件 网络安全
阿里云轻量应用服务器的使用限制
阿里云轻量应用服务器的使用限制
|
4天前
阿里云国际版购买了服务器后如何下载发票?
阿里云国际版购买了服务器后如何下载发票?
|
5天前
|
弹性计算 Linux 数据库
阿里云国际版如何迁移Linux云服务器系统盘中的数据
阿里云国际版如何迁移Linux云服务器系统盘中的数据
|
5天前
|
弹性计算 网络协议 Ubuntu
如何在阿里云国际版Linux云服务器中自定义配置DNS
如何在阿里云国际版Linux云服务器中自定义配置DNS
|
5天前
|
弹性计算 安全 Linux
阿里云国际版使用ping命令测试ECS云服务器不通的排查方法
阿里云国际版使用ping命令测试ECS云服务器不通的排查方法