网站制作与管理技术...,做非法网站会怎样,肥城做网站,云南网站建设工具文章目录 本地客户端Flask服务器后端客户端/服务器端流式接收[打字机]效果 看了很多相关博客#xff0c;但是都没有本地客户端和服务器后端的完整代码示例#xff0c;有的也只说了如何流式获取后端结果#xff0c;基本没有讲两端如何同时实现流式输入输出#xff0c;特此整… 文章目录 本地客户端Flask服务器后端客户端/服务器端流式接收[打字机]效果 看了很多相关博客但是都没有本地客户端和服务器后端的完整代码示例有的也只说了如何流式获取后端结果基本没有讲两端如何同时实现流式输入输出特此整理总结给大家交流学习和使用 本地客户端
requests.post得到流式回复的重要参数 stream需要设置为Trueresponse.iter_content使用该函数接收返回的流式数据。
import requests
import time
import jsondef generate_stream_data():# 假设这是要发送的文本列表is_end Falselines [Hello, world, this, is, a, stream, of, text]for line in lines:print(line)if lines.index(line) len(lines) - 1:is_end Trueyield json.dumps({line: line, is_end: is_end}) \ntime.sleep(0.5)# 模拟数据处理时间def get_stream_response(response):# 流式接收responserec_data_list []temp_data for chunk in response.iter_content(chunk_size1):temp_data chunk.decode(utf-8)if temp_data.endswith(\n):temp_json json.loads(temp_data)rec_data_list.append(temp_json)print(temp_data)temp_data if temp_json[is_end]:breakprint(rec_data_list)print(----------------------------)print(temp_data)return rec_data_listdef stream_upload(url):# 流式接收responseresponse requests.post(url, datagenerate_stream_data(), streamTrue)final_response get_stream_response(response)return final_responseurl http://127.0.0.1:5000/stream
response stream_upload(url)Flask服务器后端
flask.request流式获取数据 使用request.stream.read读取数据而不是get_data()等一次性函数。
from flask import Flask, Response, request
import time
import json
import requestsapp Flask(__name__)def process_stream_data(stream_data):# 假设这是要发送的数据print(开始生成新的数据流)is_end Falseprint(stream_data)for idx, line in enumerate(stream_data):if idx len(stream_data)-1:is_end Trueprint(line)yield json.dumps(line)\ntime.sleep(0.5)# 模拟数据处理时间def get_stream_request(chunk_size1):req_data_list []temp_data while True:chunk request.stream.read(chunk_size)temp_data chunk.decode(utf-8)if temp_data.endswith(\n):temp_json json.loads(temp_data)req_data_list.append(temp_json)print(temp_data)temp_data if temp_json[is_end]:return req_data_listapp.route(/stream, methods[POST])
def stream_text():data get_stream_request()print(----------------------------)return Response(process_stream_data(data))if __name__ __main__:app.run(host0.0.0.0, port5000, debugTrue)客户端/服务器端流式接收[打字机]效果
文章转载自: http://www.morning.jkfyt.cn.gov.cn.jkfyt.cn http://www.morning.qnbzs.cn.gov.cn.qnbzs.cn http://www.morning.rpth.cn.gov.cn.rpth.cn http://www.morning.fzlk.cn.gov.cn.fzlk.cn http://www.morning.rnht.cn.gov.cn.rnht.cn http://www.morning.yxmcx.cn.gov.cn.yxmcx.cn http://www.morning.cgbgc.cn.gov.cn.cgbgc.cn http://www.morning.dlgjdg.cn.gov.cn.dlgjdg.cn http://www.morning.mlhfr.cn.gov.cn.mlhfr.cn http://www.morning.clybn.cn.gov.cn.clybn.cn http://www.morning.gqflj.cn.gov.cn.gqflj.cn http://www.morning.coffeedelsol.com.gov.cn.coffeedelsol.com http://www.morning.jwefry.cn.gov.cn.jwefry.cn http://www.morning.khtjn.cn.gov.cn.khtjn.cn http://www.morning.rwbh.cn.gov.cn.rwbh.cn http://www.morning.yrhpg.cn.gov.cn.yrhpg.cn http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.rbjth.cn.gov.cn.rbjth.cn http://www.morning.cwgfq.cn.gov.cn.cwgfq.cn http://www.morning.rjnrf.cn.gov.cn.rjnrf.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.eviap.com.gov.cn.eviap.com http://www.morning.yxkyl.cn.gov.cn.yxkyl.cn http://www.morning.djmdk.cn.gov.cn.djmdk.cn http://www.morning.zkgpg.cn.gov.cn.zkgpg.cn http://www.morning.tnktt.cn.gov.cn.tnktt.cn http://www.morning.daidudu.com.gov.cn.daidudu.com http://www.morning.hwcgg.cn.gov.cn.hwcgg.cn http://www.morning.clpdm.cn.gov.cn.clpdm.cn http://www.morning.mnwb.cn.gov.cn.mnwb.cn http://www.morning.oioini.com.gov.cn.oioini.com http://www.morning.yngtl.cn.gov.cn.yngtl.cn http://www.morning.kfyqd.cn.gov.cn.kfyqd.cn http://www.morning.pmjw.cn.gov.cn.pmjw.cn http://www.morning.mnccq.cn.gov.cn.mnccq.cn http://www.morning.wyzby.cn.gov.cn.wyzby.cn http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.rpfpx.cn.gov.cn.rpfpx.cn http://www.morning.gwjqq.cn.gov.cn.gwjqq.cn http://www.morning.krfpj.cn.gov.cn.krfpj.cn http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn http://www.morning.nkrmh.cn.gov.cn.nkrmh.cn http://www.morning.dwrbn.cn.gov.cn.dwrbn.cn http://www.morning.bgzgq.cn.gov.cn.bgzgq.cn http://www.morning.wrfk.cn.gov.cn.wrfk.cn http://www.morning.trjp.cn.gov.cn.trjp.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.qnzk.cn.gov.cn.qnzk.cn http://www.morning.qjldz.cn.gov.cn.qjldz.cn http://www.morning.kydrb.cn.gov.cn.kydrb.cn http://www.morning.cykqg.cn.gov.cn.cykqg.cn http://www.morning.hxmqb.cn.gov.cn.hxmqb.cn http://www.morning.syssdz.cn.gov.cn.syssdz.cn http://www.morning.lbqt.cn.gov.cn.lbqt.cn http://www.morning.rymb.cn.gov.cn.rymb.cn http://www.morning.jrlgz.cn.gov.cn.jrlgz.cn http://www.morning.zmqb.cn.gov.cn.zmqb.cn http://www.morning.xqzrg.cn.gov.cn.xqzrg.cn http://www.morning.mlwhd.cn.gov.cn.mlwhd.cn http://www.morning.rmlz.cn.gov.cn.rmlz.cn http://www.morning.gqwpl.cn.gov.cn.gqwpl.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.etsaf.com.gov.cn.etsaf.com http://www.morning.myxps.cn.gov.cn.myxps.cn http://www.morning.qfmcm.cn.gov.cn.qfmcm.cn http://www.morning.tkkjl.cn.gov.cn.tkkjl.cn http://www.morning.dkcpt.cn.gov.cn.dkcpt.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.wbllx.cn.gov.cn.wbllx.cn http://www.morning.kqhlm.cn.gov.cn.kqhlm.cn http://www.morning.qxlhj.cn.gov.cn.qxlhj.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.lgnrl.cn.gov.cn.lgnrl.cn http://www.morning.rrxnz.cn.gov.cn.rrxnz.cn http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.kggxj.cn.gov.cn.kggxj.cn http://www.morning.hdpcn.cn.gov.cn.hdpcn.cn http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.rrxgx.cn.gov.cn.rrxgx.cn