Article Directory
Developer Server
- Only for servers used for development of
- It can not be used in the actual project
- advantage
- More friendly to developers
- Automatic restart
- Rich log
- Debugging page
- Shortcoming
- Low performance
The method of HTTP request
-
GET
- Acquisition, take
- Obtain data from a server used, access to resources
-
POST
- submit
- To submit transfer (submit) to the server
- Most used in the creation, validation
-
DELETE
- delete
- Tell the server, I want to delete a resource
-
PUT
- put
- submit
- Used to submit data to the server
- PUT data to update typically
-
PATCH
- patch
- submit
- Used to submit data to the server
- Typically the amount of difference update data used PATCH
Flask setting request method
- Default get support request
- I want to support custom request
@app.route("/xxx/", methods=["GET", "POST"])
Router router acquisition
# python中的写法
@app.route("/register/", methods=["GET", "POST"])
def register():
return
@app.route("/doregister/", methods=["GET", "POST"])
# 路由返回的是字符串,可以在返回值里面直接加标签代码:<a>lable</a>
def do_register():
return
{#html 中的写法#}
<a href="register">login</a>
<form action="/doregister/" method="post">
Flask of View
-
There are two important objects in View
-
Double R
- Request
- A request sent by a client
- Which contains a variety of client information
- Request client is not created
- Framework is created based on the client's data (request packet)
- Belonging to the built-in objects in Flask, can not be modified
- Life Cycle Request: Request from the beginning to the end of Response
- Request Properties
- full request url address
- base url removed GET URL parameters
- Only host url URL host and port numbers
- path request path
- method request method
- remote addr requesting client address
- args GET request parameters
- form POST request parameters
- File upload files
- request header headers
- cookies in the cookie request
- Response
- The server returns data to the client
- Created by the developer
- There is a variety of ways to create
- Returns a string directly
return 'log in success'
- Templates can be returned
return render_template('login.html')
- Direct Response object is returned
- Created directly Response ()
- May also be used make_response (data, code)
- The first parameter is the content, the second parameter is the status code
- Returns a string directly
- Request
-
Type of response
-
Intuitive response to the request
return render_template('index.html')
-
Redirect request
- redirect ( '/') redirected to the root directory (Home)
- url_for dynamic IP address
redirect(url_for('do_register'))
do_register is defined routing router
-
Active termination request
abort(404)
abort(Response('Hello World'))
# Response of a WSGI application package
-
Catch the exception
-
@app.errorhandler(404) def hello(e): return 'LOL'
-
Capture POST and GET requests
- Obtaining parameters via POST
username = request.form.get('username')
Parameters passed in a form by form
- Obtaining parameters via GET
username = request.args.get('username')
Args parameter passed by
- Analyzing Request
if request.method == 'GET'
if the request to process the flow control
-
Technical Session
-
Request cycle is very short
-
HTTP stateless protocol
- No protocol for transaction processing and memory
-
HTTP connection is short
- The client and server once for each HTTP operation, once the connection is established, the task is interrupted connection end
- From HTTP / 1.1 from the default length using TCP connections maintained, for holding connection characteristics
- The client and server once for each HTTP operation, once the connection is established, the task is interrupted connection end
-
classification
-
Cookie
- Client session technology
- Response written by browser, data is stored in the client
response.set_cookie(key,value[max_age=None,exprise=None])
request.cookie.get(key)
- characteristic
- Cookie default request will carry all of this site
- Cookie can not cross-site, different domain, different ip
- Cookie can not cross-browser
- Cookie stored in key-value
- Cookie Support expired
- Flask default browser closed, Cookie expired
- max_age: integer specifying the cookie expiration time, None never expires
- expries: integer, can specify a specific date (alternative)
- Delete cookie
response.delete_cookie(key)
- Shortcoming
- Unsafe
- Cleartext
-
Session
-
Session server technology
-
Data is stored on the server
-
characteristic
-
Dependent on Cookie
-
Session key-value store to
- Set key-value pairs
session['username'] = 'value'
- Get key-value pairs
get(key,default=None)
- Delete key-value pairs
pop(key)
- Clear all
clear()
- Set key-value pairs
-
Relatively safe
app.config["SECRET_KEY"] = "secret_key"
-
You can not cross-site, can not be cross-domain
-
Support expired
-
session.permanent = True # permanent_session_lifetime设置过期时间 session.permanent_session_lifetime = timedelta(days=1)
-
-
-
-
Token (does not support Cookie scene)
- Phone App
- Session server technology
- Custom server-side session technology
- Data storage, query, modify, token (the unique identifier, session_key, session_id) issued entirely controlled by the server
-
Session scene
- Need to request the presence data across
- Extend the life cycle of the data request