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

网站服务器租赁需要什么手续品牌网店怎么加盟

网站服务器租赁需要什么手续,品牌网店怎么加盟,电子商务网站管理,自己建的网站也要注册域名吗基于Flask、Flasgger、marshmallow的开发调试 问题描述调试方法一调试方法二调试方法三 问题描述 现在有一个传入传出为json格式文件的#xff0c;Flask-restful开发的程序#xff0c;需要解决如何调试的问题。 #!/usr/bin/python3 # -*- coding: utf-8 -*- # Project :… 基于Flask、Flasgger、marshmallow的开发调试 问题描述调试方法一调试方法二调试方法三 问题描述 现在有一个传入传出为json格式文件的Flask-restful开发的程序需要解决如何调试的问题。 #!/usr/bin/python3 # -*- coding: utf-8 -*- # Project : combine all libraries examples.py # File : RiGangTemplateTry.py # Time : 2024/1/4 8:43 from flask import Flask, request from flask_restful import Api, Resource from flasgger import Swagger from marshmallow import Schema, fields, ValidationError# 初始化 Flask 应用、API 和 Swagger app Flask(__name__) api Api(app) swagger Swagger(app)# 校验输入数据 class AgeSchema(Schema):name fields.Str(requiredTrue)age fields.Integer(requiredTrue)# 校验输出数据 class AgeStatSchema(Schema):average_age fields.Float()max_age fields.Integer()min_age fields.Integer()class AgeStats(Resource):def post(self):Upload JSON and Calculate Age Stats---consumes:- application/jsonparameters:- in: bodyname: bodyschema:id: AgeInputtype: arrayitems:type: objectrequired:- name- ageproperties:name:type: stringage:type: integerdescription: JSON array with names and agesresponses:200:description: Age statisticsschema:id: AgeStatsproperties:average_age:type: numberformat: floatdescription: The average age of the submitted agesmax_age:type: integerdescription: The maximum age of the submitted agesmin_age:type: integerdescription: The minimum age of the submitted agesjson_data request.get_json()# 校验 JSON 数据try:results AgeSchema(manyTrue).load(json_data)except ValidationError as err:return err.messages, 400# 计算平均年龄、最大年龄和最小年龄ages [person[age] for person in results]average_age sum(ages) / len(ages)max_age max(ages)min_age min(ages)# 序列化输出数据stats_schema AgeStatSchema()return stats_schema.dump({average_age: average_age,max_age: max_age,min_age: min_age}), 200class UserSchema(Schema):username fields.Str(requiredTrue)email fields.Email(requiredTrue)class User(Resource):def get(self, username):Get User Endpoint---parameters:- in: pathname: usernametype: stringrequired: truedescription: The username of the userresponses:200:description: The user informationschema:id: UserResponseproperties:username:type: stringdescription: The username of the useremail:type: stringdescription: The email of the userexamples:application/json: { username: johndoe, email: johnexample.com }# 示例数据实际应用中这里会是数据库查询等操作user_data {username: username, email: f{username}example.com}# 使用 Marshmallow Schema 校验和序列化数据user_schema UserSchema()return user_schema.dump(user_data), 200api.add_resource(User, /users/string:username) api.add_resource(AgeStats, /age_stats)if __name__ __main__:app.run(debugTrue) 调试方法一 通过http://127.0.0.1:5000/apidocs/已经可以便捷的查看代码中的api数据。 但是在测试代码的时候仍然需要手动输入调试json数据在界面上 调试方法二 要使用您的Flask应用进行测试您可以采用以下步骤 确保您的环境已经安装了所有必需的库。如果还没有安装您可以使用pip来安装它们 pip install flask flask-restful flasgger marshmallow保存并运行您的Flask应用。将您的脚本保存为一个.py文件例如app.py然后在命令行中运行它 python app.py准备您的测试数据。创建一个JSON文件data.json包含您想要测试的数据例如 [{name: Alice, age: 30},{name: Bob, age: 25},{name: Charlie, age: 35} ]使用curl命令或者Postman等工具发送请求。 使用curl发送POST请求 curl -X POST -H Content-Type: application/json -d data.json http://127.0.0.1:5000/age_stats确保您的Flask应用正在运行并且使用了data.json文件中的正确路径。 如果您更喜欢图形界面可以使用Postman 打开Postman。创建一个新的POST请求。在URL栏输入http://127.0.0.1:5000/age_stats。在Headers部分添加一个新的条目。对于key填入Content-Type对于value填入application/json。在Body部分选择raw然后从下拉菜单中选择JSON。将data.json文件中的数据复制并粘贴到raw文本区域中。点击Send。 观察响应。无论是curl还是Postman您都应该收到一个包含平均年龄、最大年龄和最小年龄的JSON响应。 调试。如果测试没有按预期进行您可以在Flask应用中添加print语句或使用Python的pdb模块来调试。您还可以检查Postman或终端中的错误信息来帮助诊断问题。 如果您遇到400 Bad Request错误这通常意味着您的输入数据不符合AgeSchema的要求。在这种情况下检查您的JSON数据确保每个对象都有name和age字段并且age是一个整数。 调试方法三 写一个调试的脚本使用Request调试 #!/usr/bin/python3 # -*- coding: utf-8 -*- # Project : combine all libraries examples.py # File : TestRiGangTemplateTry.py # Time : 2024/1/4 9:15import requests import json# 设置您的API端点 url http://127.0.0.1:5000/age_stats# 准备您的测试数据 data [{name: Alice, age: 30},{name: Bob, age: 25},{name: Charlie, age: 35} ]# 将数据转换为JSON格式 json_data json.dumps(data)# 发送POST请求 response requests.post(url, datajson_data, headers{Content-Type: application/json})# 打印响应 print(Status Code:, response.status_code) print(Response Body:, response.text)
http://www.dnsts.com.cn/news/44183.html

相关文章:

  • 做网站把自己做死惠州网站推广
  • 景德镇网站网站建设全球新冠死亡人数
  • 甘肃省建设厅安全员官方网站网站设计推广方案
  • 房地产行业最新消息某一网站seo策划方案
  • 网站招商页面怎么做外贸网站 wordpress
  • 网站建设销售实习报告安卓开发课程
  • 一个旅游网站怎么做网站建设的法律问题
  • 广东省住房建设厅网站做彩铃的网站
  • 花生壳做网站是否稳定软文推广公司
  • 户外网站模板南宁seo教程
  • 网站欢迎屏怎么做写作网站水平哪个最好
  • 上海网站建站模板做代码和网站
  • 个人做电商网站赚钱吗seo入门黑帽培训教程
  • 如何做网站搜索排名沈阳app开发公司哪家好
  • 南京百度推广网站国有平台公司是什么意思
  • 物流网站系统php源码微信公众号如何创建视频链接
  • 深圳市住房建设部网站wordpress视频防止下载文件
  • 网站不收录专业团队的重要性
  • 做外贸网站需要缴什么税html注册登录模板
  • 湛江制作网站多少钱公司门户网站及oa系统建设的请示
  • 教学网站开发背景西安官网seo诊断
  • 广州企业所得税河北seo网站优化公司
  • 建个网站多少钱app贵州省住房和城乡建设厅网站官网
  • 网站建设服务标准深圳罗湖网站制作
  • 做企业福利网站起名怎么做公司网站的二维码
  • 网站开发各小组互评表html5 开发的网站
  • 做设计需要知道的几个网站吗注册一个互联网公司需要多少钱
  • wordpress建站 知乎北京做网站建设的公司有哪些
  • 雅奇小蘑菇做网站好不好用怎么在自己做网站
  • 网站设计风格类型wordpress函数文件夹