flask- promoted to Senior

A cache

  • flask-caching

 

  1. Cache data
  2. Reduce disk IO, improve server responsiveness, enhance the user experience
  3. Preference level cache memory
  4. This memory-level database using Redis
 1 from flask import Flask
 2 from flask_caching import Cache
 3 
 4 app = Flask(__name__)
 5 
 6 cache_info = {
 7         "CACHE_TYPE": "redis",
 8         "CACHE_REDIS_URL": "redis://:123456@localhost:6379/2"
 9     }
10 
11 cache = Cache()
12 cache.init_app(app, config=cache_info)
13 
14 @app.route('/')
15 @cache.cached(timeout=60)
16 def hello_world():
17     return 'Hello World!'
18 
19 if __name__ == '__main__':
20     app.run(host='0.0.0.0', debug=True)

 

Second, file upload

  1. It will deliver a client's files to the server
  2. Essentially a file copy
  3. Data transmission network
  4. The client must first submit a request type POST

First, in the form tag html page plus attribute enctype = "multipart / form-data", this must surely remember plus.

1 <form action="" method="post" enctype="multipart/form-data">

 

Remove the file operations:

1 file = request.files.get("file_name")

 

Stored on disk:

. 1 file_path = " path to store the file " 
2 File.Save (file_path)

 

Third, send e-mail

  • flask-mail

Directly on the code, you understand at a glance:

 

. 1  from Flask Import the Flask
 2  from flask_mail Import Mail, the Message
 . 3  
. 4 App = the Flask ( the __name__ )
 . 5  
. 6 the app.config [ " MAIL_SERVER " =] " smtp.163.com "   # mail server address, in view there is provided, which is example 
. 7 the app.config [ " MAIL_USERNAME " ] = " ******@163.com " 
. 8 the app.config [ " mail_password " ] = " *************************************** " 
. 9 
10 mail = Mail()
11 mail.init_app(app)
12 
13 @app.route('/')
14 def hello():
15     content = "<h1>小伙子你开什么车?</h1>"
16     msg = Message("Hello", sender="******@163.com", recipients=["******@163.com", ], html=content)
17     mail.send(msg)
18     return "Hello World"
19 
20 if __name__ == '__main__':
21     app.run(host='0.0.0.0', debug=True)

Guess you like

Origin www.cnblogs.com/wuzc/p/12052093.html