简体   繁体   English

如何将“flask”配置为仅从浏览器运行文件?

[英]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?如何配置“flask”,以便您可以在服务器上的文件夹中创建一个包含必要代码的文件并从浏览器中调用它,例如 PHP?

Example: http://example.com/my_flask_file_with_python_code.py示例: 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. OP 希望能够在遇到特定 URL 时执行 python 脚本 - 可能通过 Flask。

Two possible ways I can think of我能想到的两种可能的方法

Option 1选项1

  1. Use some production grade webserver - like Apache or Nginx, in front of your Flask app.在您的 Flask 应用程序前面使用一些生产级网络服务器 - 例如 Apache 或 Nginx。
  2. And then, write CGI scripts (in Python) - which will directly run when you hit a particular request.然后,编写 CGI 脚本(在 Python 中)——当您遇到特定请求时,它将直接运行。 You can possibly follow this tutorial for the same - https://www.tutorialspoint.com/python/python_cgi_programming.htm您可以按照本教程进行相同的操作 - https://www.tutorialspoint.com/python/python_cgi_programming.htm

Option 2选项 2

  1. Get the URL from location and based on the name, simply launch scripts and return the response.从位置获取 URL 并根据名称,只需启动脚本并返回响应。

  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. PS我不会推荐这个,只是为了完整起见在这里说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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