如何在 HTML 中运行 python 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40844903/
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
How to run python script in HTML?
提问by Ann
Currently I'm having some python files which connects to sqlite database for user inputs and then performs some calculations which sets the output of the program. I'm new to python web programming and I want to know what is the best method to use python on web?
目前我有一些 python 文件,它们连接到 sqlite 数据库以供用户输入,然后执行一些设置程序输出的计算。我是 python 网络编程的新手,我想知道在网络上使用 python 的最佳方法是什么?
Ex: I want to run my python files when the user clicks a button on the web page. Is it possible?
例如:当用户单击网页上的按钮时,我想运行我的 python 文件。是否可以?
I started with Django. But it needs some time for the learning. And I also saw something called CgiScripts. Which option should I use? Please advice.
我从 Django 开始。但是需要一定的时间来学习。我还看到了一个叫做 CgiScripts 的东西。我应该使用哪个选项?请指教。
采纳答案by abacles
It probably would depend on what you want to do. I personally use CGI and it might be simpler if your inputs from the web page are simple, and it takes less time to learn. Here are some resources for it:
这可能取决于你想做什么。我个人使用 CGI,如果您从网页输入的内容简单,那么学习可能会更简单,并且学习所需的时间更少。以下是它的一些资源:
- https://docs.python.org/2/library/cgi.html
- https://www.tutorialspoint.com/python/python_cgi_programming.htm
- There's a simple example in the end of this one although it mainly focuses on HTML forms: http://abacles.com/old/html-forms.html
- https://docs.python.org/2/library/cgi.html
- https://www.tutorialspoint.com/python/python_cgi_programming.htm
- 虽然它主要关注 HTML 表单,但在这个末尾有一个简单的例子:http: //abacles.com/old/html-forms.html
However you may still have to do some configuring to allow it to run the program instead of displaying it.
但是,您可能仍然需要进行一些配置以允许它运行程序而不是显示它。
Here's a tutorial on that: http://httpd.apache.org/docs/current/howto/cgi.html
回答by Mani Kandan
You can able to run a python file using html using php
您可以使用 html 使用php运行 python 文件
write a PHP file as index.php:
编写一个 PHP 文件作为 index.php:
<html>
<head>
<title>run my python files</title>
<?PHP
echo shell_exec("python test.py 'parameter1'");
?>
</head>
passing the parameter to python create a python as test.py:
将参数传递给 python 创建一个 python 作为 test.py:
import sys
input=sys.argv[1]
print(input)
print the parameter passed by PHP.
打印 PHP 传递的参数。
回答by Danny Cullen
If your web server is apache you can use the http://modpython.org/module in order to run your python CGI scripts.
如果你的 web 服务器是 apache,你可以使用http://modpython.org/模块来运行你的 python CGI 脚本。
For nginx you can use http://modwsgi.readthedocs.io/en/develop/
对于 nginx,您可以使用http://modwsgi.readthedocs.io/en/develop/
回答by Hemel
You can't run python directly
你不能直接运行python
You may use this
你可以用这个
http://karrigell.sourceforge.net/en/pythoninsidehtml.html
Or for inside php this
或者在 php 这个里面
http://www.skulpt.org/