当前位置:网站首页>Flash basic code
Flash basic code
2022-04-22 01:21:00 【NLstudy33】
# 1. from flask package / modular Introduction in flask class
from flask import Flask,current_app,g,request,session,make_response
from flask import render_template,redirect,abort
app = Flask(__name__)
app.debug = True
# current_app Examples of current applications
# g Temporary storage object for processing requests , This variable is reset on every request
@app.route('/index')
def index():
print('app:',app)
print('current_app',current_app) # current_app Examples of current applications
print(app == current_app) # True It's just that the values are equal , Because we have only one app start-up , Not many
print(app is current_app) # False Quotations are different
return 'index'
@app.route('/')
def helloworld():
# The view function
# return 'hello world,susede'
# visit '/' when , Redirect to '/test' page
# return redirect('/test')
# abort(403) # Triggering an exception , Unwanted return, Will call the following forbidden function
# ip Intercept
ip_list = ['127.0.0.2']
ip = request.remote_addr
if ip in ip_list:
abort(403) # Will call the following forbidden function
return 'hello,sucess,welcome'
# Routing configuration uses decorators @app.route(url,methods)
@app.route('/test')
def hellotest():
# The view function
return 'hello world,test0222, Test functions '
# URL Get parameters in view page
@app.route('/user/')
@app.route('/user/<page>')
def list_user(page=1):
return ' Hello , I am the first {} Page user '.format(page)
# Use API To configure app.add_url_rule(url,url_name,view_name)
app.add_url_rule('/test-apimethod','apimethod',hellotest)
# see url Routing rules
print(app.url_map)
@app.route('/test/req')
def test_request():
''' Request message exercise '''
get_args = request.args
print('get_args',get_args)# get Request parameter acquisition
print(get_args.get('page',1)) # No... Was passed in the request page value , Then you can set the default value
print(request.args.get('name'))
# Get the host where the server is located IP Address
headers = request.headers
print(headers)
print('host',headers.get('host'))
# Get the ip Address
user_ip = request.remote_addr
print('user_ip',user_ip)
print(request.remote_user)
# obtain user-agent, It can be used for reptiles
user_agent = request.headers.get('user-agent',None)
print('User-Agent:','\n',user_agent)
return 'request success'
@app.before_first_request
def first_request():
""" After the server starts, before the first request arrives """
print(' After the server starts, before the first request arrives ,first_request')
@app.before_request
def per_request():
""" Before every request arrives """
print(' Before every request arrives ,per_request')
# @app.after_request
# def after_request(reponse):
# """ Execute after the request , If an exception occurs in the process, it will not be executed """
# print(' Execute after the request , If an exception occurs in the process, it will not be executed ,after_request')
# print(reponse.get_json())
# return 'after_request'
@app.route('/test/resp')
def test_response():
""" Test response """
return ' Response test ,reponse success',405,{
'user_name':'mark','user_id':'mark_id2'}
@app.route('/test/mkresp')
def test_mkresponse():
''' Use make_response Construct the response object '''
mkresp = make_response(' This is a response object ',403,{
'token':'abc123'
})
mkresp.headers['user_name'] = 'nick_li'
mkresp.status_code = 200
return mkresp
@app.route('/test/htmlresp')
def test_html_response():
''' Respond to html'''
html = '<html><body><h1 style="color:red"> This is a HTML Response test </h1></body></html>'
resp = make_response(html)
return resp
@app.route('/test/html')
def test_html():
''' Read from file html'''
html = render_template('test1.html')
resp = make_response(html,400) # Can refer to the previous use
return resp
@app.errorhandler(403)
def forbidden_page(err):
"""403 Error message : You don't have access to the page """
print(err)
return ' You don't have access to the page , Please contact the administrator to open the permission '
# if __name__ == '__main__':
# app.run()
版权声明
本文为[NLstudy33]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220055101071.html
边栏推荐
- js查找数组下标
- flask-基础代码
- April 21, 2022, day 14
- From the perspective of source code, what are the ways to create thread pool
- Principle and application of closure
- 贼厉害,最新Android面试合集
- Redis personal notes: redis application scenario, redis common commands, redis cache breakdown and penetration, redis distributed lock implementation scheme, spike design idea, redis message queue, Re
- 简单理解变量的结构赋值
- Oceanbase 和 TiDB 粗浅对比之 - 执行计划
- 3D 沙盒游戏之人物的点击行走移动
猜你喜欢

This problem occurs if you are running in 64 bit mode with 32-bit Oracle client components installed

The middle order traversal of binary tree

【PraNet】主要架构解读-------wmilk

EventBridge 集成云服务实践

Shallow comparison between oceanbase and tidb - implementation plan

Oceanbase 和 TiDB 粗浅对比之 - 执行计划

华为云云主机体验有感:你的软件收纳专家

How does PR adjust the exported data rate and total bit rate

网络模型 LSTM模型内容详解

Eventbridge integrated cloud service practice
随机推荐
Force buckle 55 Jumping game
【Pranet】论文及代码解读(ResNet部分)——jialiang nie
OTS parsing error: invalid version tag solution
PR如何调整导出的数据速率和总比特率
闭包的原理及应用
MySQL创建远程登陆用户并授权
Vnpy chapter of quantitative trading - synchronization module avoids self transaction risk and adds synchronization completion prompt
Rpcx source code learning - server side
Pytoch installation and troubleshooting of groupspatialsoftmax errors
Set the countdown of 5 seconds in setTimeout
【Pranet】论文及代码解读(Res2Net部分)——peiheng jia
Kotlin - 高阶函数介绍
谈谈SWEN20003软件开发技巧
Finally, wechat scanning code login is completed. It's really fragrant..
Live broadcast of goods, delivery of takeout and freight transportation. Airlines rely on sidelines to "return blood"
Principle and application of closure
Solve the problem that the idea web project does not have small blue dots
Griddata layout description
The AOV function of R language is used for one-way ANOVA, the normality of the dependent variable of ANOVA is evaluated by Q-Q diagram, the equality (homogeneity) of Bartlett's verification variance,
Draw2d custom dashboard