当前位置: 首页 > news >正文

那些空号检测网站是怎么做的cms建站

那些空号检测网站是怎么做的,cms建站,中国建设布网站,网站建设和实现论文文章目录 本地客户端Flask服务器后端客户端/服务器端流式接收[打字机]效果 看了很多相关博客,但是都没有本地客户端和服务器后端的完整代码示例,有的也只说了如何流式获取后端结果,基本没有讲两端如何同时实现流式输入输出,特此整…

文章目录

    • 本地客户端
    • Flask服务器后端
    • 客户端/服务器端流式接收[打字机]效果

看了很多相关博客,但是都没有本地客户端和服务器后端的完整代码示例,有的也只说了如何流式获取后端结果,基本没有讲两端如何同时实现流式输入输出,特此整理总结,给大家交流学习和使用!

本地客户端

  • requests.post得到流式回复的重要参数:
    • stream:需要设置为True;
    • response.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}) + '\n'time.sleep(0.5)# 模拟数据处理时间def get_stream_response(response):# 流式接收responserec_data_list = []temp_data = ''for chunk in response.iter_content(chunk_size=1):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, data=generate_stream_data(), stream=True)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)+"\n"time.sleep(0.5)# 模拟数据处理时间def get_stream_request(chunk_size=1):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_list@app.route('/stream', methods=['POST'])
def stream_text():data = get_stream_request()print("----------------------------")return Response(process_stream_data(data))if __name__ == "__main__":app.run(host='0.0.0.0', port=5000, debug=True)

客户端/服务器端流式接收[打字机]效果

请添加图片描述

http://www.ds6.com.cn/news/34616.html

相关文章:

  • 网站开发图书管理系统成都关键词优化服务
  • 聊城质量最好网站建设竞价外包推广专业公司
  • 网站建设600元包常用的seo工具的是有哪些
  • 网站开发适合女生干吗关键词排名工具有哪些
  • 做返利网站能赚钱电脑培训学校能学什么
  • 网站四对联广告代码深圳网络优化推广公司
  • 网站做友链爱站网seo工具包
  • 一建报名资格条件附子seo
  • wordpress缩进去的边栏宁波网站建设优化企业
  • 个人博客网站建设东莞推广
  • 网站建设百度百科西安seo优化工作室
  • wordpress迁移跳转原网站品牌营销推广
  • 工作站做网站第三方网络营销平台有哪些
  • 2015做哪些网站致富郑州模板网站建设
  • h5页面是什么海洋seo
  • 美橙网站注册广西疫情最新消息
  • 上海网站建设公司站霸网络注册城乡规划师
  • 在百度上做个网站要多少钱百度ocpc怎么优化
  • 优惠券购物网站怎么做微信推广广告在哪里做
  • 郑州建网站十大搜索推广开户
  • 给你网站你会怎么做百度竞价推广的技巧
  • 今日头条网站模板百度收录网址
  • 电子商务网站建设课程总结凡科建站下载
  • 重庆宣传片制作seo营销推广公司
  • 多用户商城系统哪里有seo引擎搜索网址
  • 网站开发知识版权推推蛙seo顾问
  • 获取文章内容 wordpressgoogle关键词seo
  • 在线diy网站国内最新消息新闻
  • 建立企业网站的缺点网络seo
  • 做58一样的网站百度官网电话客服24小时