zoukankan      html  css  js  c++  java
  • Python-Flask请求

    1.get请求

    ①.通过地址栏?key=value
    from flask import Flask,request
    @app.route(
    '/get1', methods=['GET']) def M1(): """①.地址栏?key=value取值""" name = request.args.get('name', '')#姓名 age = request.args.get('age', '')#年龄 return str(age) + "/" + name

    效果:

    ②.通过地址栏
    from flask import Flask,request
    
    @app.route('/get2/<int:age>/<name>',methods=['GET'])
    def M2(age,name):
        """②.地址栏取值"""
        return str(age) + "/" + name

    效果:

    2.post请求

    ①.Form表单
    import os
    from flask import Flask,request 
    from werkzeug.utils import secure_filename
    
    @app.route('/post1',methods=['POST'])
    def M3():
        """①.Form表单取值"""
        name = request.form['name']#姓名
        age = request.form['age']#年龄
        #file对象
        file = request.files['file']
        #保存图片
        file.save(os.path.join(os.getcwd(), secure_filename(file.filename)))
        return  str(age) + "/" + name + "/" + file.filename

    效果:

    图片:

    ②.Json字符串

    from flask import Flask,request
    
    @app.route('/post2',methods=['POST'])
    def M4():
        """①.Json字符串取值"""
        user = eval(request.data)
        result = str(user['age'])#年龄
        result += "/" + user['name']#姓名
        #teach对象
        teach = user['teach']
        #循环对象
        for t in teach:
            result+="/" + t['class'] 
            #循环数组
            for n in t['num']:
                result+= "/" + str(n)
        return result

    效果:

     

  • 相关阅读:
    POJ 3278 Catch That Cow(BFS)
    POJ 2488 A Knight's Journey(DFS)
    POJ 2386 Lake Counting(DFS)
    迷宫问题(BFS)
    两点(DFS)
    POJ 1001 Exponentiation(大数运算)
    Java IO流01-总叙
    hdu 2065 "红色病毒"问题(快速幂求模)
    POJ 2251 Dungeon Master(BFS)
    POJ 1321 棋盘问题(DFS)
  • 原文地址:https://www.cnblogs.com/MuZiLily/p/15541226.html
Copyright © 2011-2022 走看看