Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of static Files provided in Flask

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Flask provides static file case analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

1. You can use send_from_directory to send files from a directory, which is very convenient in some cases.

From flask import Flask, request, send_from_directory # set the project root directory as the static folder, you can set others.app = Flask (_ _ name__, static_url_path='') @ app.route ('/ js/') def send_js (path): return send_from_directory ('js', path) if _ _ name__ = "_ _ main__": app.run ()

2. You can use app.send_file or app.send_static_file, but it is strongly recommended not to do so.

Because it may cause a security risk to the path provided by the user.

Send_from_directory aims to control these risks.

Finally, the preferred approach is to use NGINX or other Web servers to provide static files, which will be more efficient than Flask.

Knowledge points to add:

How to provide static files in Flask

Import os.pathfrom flask import Flask, Responseapp = Flask (_ name__) app.config.from_object (_ _ name__) def root_dir (): # pragma: no cover return os.path.abspath (os.path.dirname (_ _ file__)) def get_file (filename): # pragma: no cover try: src = os.path.join (root_dir () Filename) # Figure out how flask returns static files # Tried: #-render_template #-send_file # This should not be so non-obvious return open (src). Read () except IOError as exc: return str (exc) @ app.route ('/', methods= ['GET']) def metrics (): # pragma: no cover content = get_file (' jenkins_analytics.html') return Response (content Mimetype= "text/html") @ app.route ('/', defaults= {'path':'}) @ app.route ('/') def get_resource (path): # pragma: no cover mimetypes = {".css": "text/css", ".html": "text/html", ".js": "application/javascript",} complete_path = os.path.join (root_dir ()) Path) ext = os.path.splitext (path) [1] mimetype= mimetypes.get (ext, "text/html") content = get_file (complete_path) return Response (content, mimetype=mimetype) if _ _ name__ = ='_ main__': # pragma: no cover app.run (port=80) is it helpful for you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report