Flask Post 获取json数据,小文件实例
@app.route("/get_parserResult", methods=['POST']) def get_parserResult(): data = request.stream.read() data_dict = eval(data) print(data_dict) # return '1' return jsonify({'result': 1}) def test1(): url = 'http://127.0.0.1:5000/get_parserResult' # post调用方式 user_info = {'data': [{'filename':'abc.mp4','videodate':'20210819'},{'filename':'123.mp4','videodate':'20210820'}]} data1 = json.dumps(user_info) response = requests.post(url,data=data1) #小文件发送 @app.route('/get_audio', methods=['GET', 'POST']) def get_audio(): if request.method == 'POST': file = request.files.get('messageFile') file.save(os.path.join('D:/OTT/Sample', file.filename)) print(file.filename) return '1' def test3(): url = 'http://127.0.0.1:5000/get_audio' with open('D:/OTT/Sample/20211207/2021120601.mp4', 'rb') as file: files = {'messageFile': file} req = requests.post(url, files=files) @app.route('/update_file', methods=['GET', 'POST']) def update_file(): if request.method == 'POST': data = request.get_data().decode() args = parse.unquote(data) # name1=test&name2=测试附件.xlsx&name3=测试附件.xlsx print(args) def test3(self): headers = {'Content-type': 'application/json;charset=utf-8'} # headers = {'Content-type': 'application/x-www-form-urlencoded'} url = 'http://127.0.0.1:5000/update_file' data = { 'name1': 'test', 'name2': '测试附件.xlsx', 'name3': '测试附件.xlsx', } req = requests.post(url, data=data).content # req = requests.post(url, files=files,data=data,headers=headers).content print(req) #小文件发送 @app.route('/update_file', methods=['GET', 'POST']) def update_file(): if request.method == 'POST': file_dict = request.files print(file_dict) file_a = file_dict.get('file_a') filename_a = 'D:/test/file_a.mp4' file_a.save(filename_a) file_b = file_dict.get('file_b') filename_b = 'D:/test/file_b.mp4' file_b.save(filename_b) def test3(self): # headers = {'Content-type': 'application/json;charset=utf-8'} # # headers = {'Content-type': 'application/x-www-form-urlencoded'} url = 'http://127.0.0.1:5000/update_file' file_a = r'D:/test/file_send1.mp4' file_b = r'D:/test/file_send2.mp4' file1 = open(file_a, 'rb') file2 = open(file_b, 'rb') files = {'file_a': file1, 'file_b': file2} req = requests.post(url, files=files).content print(req) #小文件发送 @app.route('/update_file', methods=['GET', 'POST']) def update_file(): if request.method == 'POST': #接收文件 file_dict = request.files file_a = file_dict.get('file_a') filename_a = 'D:/test/file_a.mp4' file_a.save(filename_a) file_b = file_dict.get('file_b') filename_b = 'D:/test/file_b.mp4' file_b.save(filename_b) #接收变量 data = request.args.get('data') print(data) #发送文件和变量 def send_data_files(self): url = 'http://127.0.0.1:5000/update_file' file_a = r'D:/test/file_send1.mp4' file_b = r'D:/test/file_send2.mp4' file1 = open(file_a, 'rb') file2 = open(file_b, 'rb') files = {'file_a': file1, 'file_b': file2} dict1 = { 'name1': {'abc': {'cc': 22}}, 'name2': '测试附件.xlsx', 'name3': '测试附件.xlsx', } data = {'data': str(dict1)} req = requests.post(url, params=data, files=files, ).content # # req = requests.post(url, files=files,data=data,headers=headers).content print(req) def getBrandbyWord(self): headers = {'Content-type': 'application/json;charset=utf-8'} # headers = {'Content-type': 'application/x-www-form-urlencoded'} url = 'http://10.20.4.187:5000/getBrand' data = { 'type': '0', 'keyword': '湖北电视台综合频道伊利原来电脑备份服务器华为' } response = requests.post(url, params=data,headers=headers).content.decode('utf8') # req = requests.post(url, files=files,data=data,headers=headers).content data_dict = json.loads(response) print(data_dict) # 将图片转换成base64编码串 def img_to_base64(self,imgFile): ''' 将图片转换成base64编码串 :param imgFile:图片文件 :return: ''' try: with open(imgFile, 'rb') as f: img_data = f.read() uri = base64.b64encode(img_data) return uri except Exception as ex: print(ex) # 图片 def getBrandbPictrue1(self): headers = {'Content-type': 'application/x-www-form-urlencoded'} url = 'http://10.20.4.21:5000/getBrand' img_path = r'D:\productData\brand\白酒\茅台.jpg' # img_path = r'D:\productData\brand\白酒\洋河.jpg' # img_path = r'D:/test/test1.jpg' base64_path = self.img_to_base64(img_path) # data = { 'type': '1', 'keyword': base64_path } # jsonstr = json.dumps(data) response = requests.post(url, data=data,headers=headers).content.decode('utf8') data_dict = json.loads(response) print(data_dict) dataType = int(request.values.get('type')) bs64data = request.form.get('keyword')