zoukankan      html  css  js  c++  java
  • request请求与响用

    一、request请求与响用

    from flask import Flask, request, make_response, render_template
    
    app = Flask(__name__)
    
    
    @app.route('/login.html', methods=['GET', "POST"])
    def login():
         # 请求相关信息
        # request.method  提交的方法,请求方式
        print(request.method)  # GET
    
        # request.args  get请求提及的数据
        print(request.args)  # ImmutableMultiDict([('age', '123')])
        print(request.args.get("age"))  # 123
    	
        # request.form   post请求提交的数据,获取表单的数据
        print(request.form)
    
        # request.values  post和get提交的数据总和
        print(request.values)    
        
        # request.cookies  客户端所带的cookie
        print(request.cookies)
        
        # request.headers  请求头
        print(request.headers) # 获取请求头User-Agent:设置爬虫拦截
        
        # request.path     不带域名,获取请求路径
        print(request.path)  # /login.html
        
        # request.full_path  不带域名,带参数的请求路径
        print(request.script_root) # 127.0.0.1 - - [16/Dec/2019 16:53:10] "GET /login.html?age=123 HTTP/1.1" 200 -
        
         # request.url           带域名带参数的请求路径
         print(request.base_url)  # 带域名请求路径 # http://127.0.0.1:5000/login.html
        
         # 域名
        print(request.url_root)  # 获取域名 http://127.0.0.1:5000/
        
        print(request.host_url)#    域名 http://127.0.0.1:5000/
        
        # 获取域名ip
        print(request.host)  # 域名ip 127.0.0.1:500
        
        # 获取前台传过来的文件
         # 获取文件
        print(request.files)
        # 获取文件对象
        obj = request.files['the_file_name']
        # 保存文件
        obj.save('/var/www/uploads/' + secure_filename(f.filename))
        
        
        # 响应相关信息
        return "字符串" # 响用字符串
        return render_template('html模板路径',**{} )  # 响应html页面,和模板语法使用
        return redirect('/index.html') # 重定向
        return jsonify({'k1':'v1'}) # json字符串
    
    	# 使用response操作cookies
        response = make_response(render_template('index.html')) # 响应对象
        # response是flask.wrappers.Response类型
        response.delete_cookie('key') # 删除cookie
        response.set_cookie('key', 'value') # 设置cookie
        response.headers['X-Something'] = 'A value'  # 设置响应头
        return response # 返回对象
    
    	# 返回字符串, 导入make_response模块
        response = make_response("内容")
        response.set_cookie("json","piao") # 设置cookie
        response.delete_cookie('json') # 删除cookie
        return response
    
    	# 返回html
        response = make_response(render_template("index.html"))
        response.headers['X-Something'] = 'sb'
        return response    
        
     
    
  • 相关阅读:
    “中国半导体教父”张汝京:中国半导体只缺人才
    集群搭建
    Scrapy
    商品建模
    python wordcloud
    StaticFileMiddleware中间件如何处理针对文件请求
    Docker / CI / CD
    NET Memory Profiler 跟踪.net 应用内存
    SOS.dll (SOS Debugging Extension)
    Download the WDK, WinDbg, and associated tools
  • 原文地址:https://www.cnblogs.com/randysun/p/15518199.html
Copyright © 2011-2022 走看看