为 HTML 网页运行 python 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15736017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:49:54  来源:igfitidea点击:

Run python script for HTML web page

pythonhtmldjangoapacheubuntu

提问by user2230425

i need to create a website. The website needs to run a Python script once the user enters the data. I have searched the net for over a week now and haven't found any help. Everybody just tells me to download DJango framework, but no body shows how to run python scripts in html web pages. I dont have any experience in web design as it is not my field. But i do know a bit of python scripting and some html. Any kind of help that puts me into right direction would be greatly appreciated.

我需要创建一个网站。一旦用户输入数据,网站就需要运行 Python 脚本。我已经在网上搜索了一个多星期,但没有找到任何帮助。每个人都告诉我下载 DJango 框架,但没有正文显示如何在 html 网页中运行 python 脚本。我在网页设计方面没有任何经验,因为它不是我的领域。但我确实知道一些 python 脚本和一些 html。任何使我进入正确方向的帮助将不胜感激。

回答by wRAR

You cannot "run python scripts in html web pages". Everyone tells you to use something like Django because if you want to make a dynamic web site that executes server-side code in response to user input you need something like Django or some other server-side web framework. So you have already been put into the right direction but ignored that.

您不能“在 html 网页中运行 python 脚本”。每个人都告诉你使用像 Django 这样的东西,因为如果你想创建一个动态的网站来执行服务器端代码以响应用户输入,你需要像 Django 或其他一些服务器端 Web 框架这样的东西。所以你已经被置于正确的方向,但忽略了这一点。

回答by spookylukey

If you wish to run Python scripts within a web page in the same way that Javascript runs within a web page, this is not possible, because web browsers don't natively understand Python.

如果您希望以与在网页中运行 Javascript 相同的方式在网页中运行 Python 脚本,这是不可能的,因为 Web 浏览器本身并不理解 Python。

If you want to run Python code that generates an HTML page, you can use a framework like Django or Flask, which will require a server that supports this kind of framework (long running processes). You can also use a CGI Python script to do this, which will require your web server to have Python installed and be set up to run CGI scripts.

如果你想运行生成 HTML 页面的 Python 代码,你可以使用像 Django 或 Flask 这样的框架,这将需要一个支持这种框架(长时间运行的进程)的服务器。您也可以使用 CGI Python 脚本来执行此操作,这将要求您的 Web 服务器安装 Python 并设置为运行 CGI 脚本。

Embedding Python in a HTML in the same way that PHP is embedded in an HTML page is generally not done in Python - it is considered an anti-pattern that leads to security problems and lots of bad practices. Python folks will generally not help you shoot yourself in the foot, unlike other communities, so you won't find much help for what is considered the wrong thing. Some template engines like Mako support using Python within templates to generate the HTML markup, but you will need to use it in conjunction with some other web framework to handle the HTTP request.

以 PHP 嵌入 HTML 页面的相同方式将 Python 嵌入 HTML 中通常不会在 Python 中完成 - 它被认为是一种反模式,会导致安全问题和许多不良做法。与其他社区不同,Python 人员通常不会帮助您采取行动,因此您不会在被认为是错误的事情上找到太多帮助。一些模板引擎(如 Mako)支持在模板中使用 Python 来生成 HTML 标记,但您需要将它与其他 Web 框架结合使用来处理 HTTP 请求。

回答by Borian

If you reallywant to run python on the client like javascript,
take a look at: Brython, skulptor pyris

如果你真的想像 javascript 一样在客户端运行 python,
看看:Brython, skulptor pyris