简体   繁体   中英

How do I configure “flask” to just run files from the browser?

How to configure "flask" so that you can just create a file with the necessary code in a folder on the server and call it from the browser, like in PHP?

Example: http://example.com/my_flask_file_with_python_code.py

I'll try to explain the question first. OP wants an ability to execute python scripts, when hitting a particular URL - possibly via Flask.

Two possible ways I can think of

Option 1

  1. Use some production grade webserver - like Apache or Nginx, in front of your Flask app.
  2. And then, write CGI scripts (in Python) - which will directly run when you hit a particular request. You can possibly follow this tutorial for the same - https://www.tutorialspoint.com/python/python_cgi_programming.htm

Option 2

  1. Get the URL from location and based on the name, simply launch scripts and return the response.

  2. Something like :

     @app.route('/scripts/<script_name>') def run_script(): proc = subprocess.Popen(["python", script_name], stdout=subprocess.PIPE, ...) proc.wait() return proc.stdout.read()
  3. PS I won't recommend this, but just stating here for sake of completeness.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM