关键模块
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)
效果如下