如何从HTML页面调用python命令
how to call python command from html page?
我想从我的html页面调用python命令。
代码为:
<form class="form" action="" method="post" name="new-service" enctype=multipart/form-data>
<div class="control-group">
<div class="controls">
<input id="Search" name="Search" type="text" placeholder="Search.. eg: moto g" class="form-control input-xlarge search-query center-display" required="">
<!-- here i want to call python command or function -->
</div>
</div>
</form>
我的python命令是:
$ python main.py -s=bow
不能直接从UI调用命令。这是特定于后端的。但是,在处理表单的视图中,您可以执行以下操作:
from django.core.management import call_command
call_command('your_command_name', args, kwargs)
查看此处的文档页面以供参考:https://docs.djangoproject.com/en/1.8/ref/django-admin/调用命令
使用jQuery向服务器上的python脚本发送请求,该请求将作为CGI/Fast CGI脚本或通过WSGI(适用于Apache的mod_wsgi)或mod_python(适用于Apache)处理。
您的Python脚本将使用查询字符串接收输入。
客户端示例:
<html><head><title>TEST</title>
<script type="text/javascript" src="jquery.js"></script>
<script>
pyurl = "/cgi-bin/main.py"; // Just for example. It can be anything. Doesn't need to be Python at all
function callpy (argdict) {
$.post(pyurl, argdict, function (data) {
// Here comes whatever you'll do with the Python's output.
// For example:
document.write(data);
});
}
</script>
</head><body>
<!-- Now use it: -->
<a href="#" onclick='javascript:callpy({"s": "bow", "someother": "argument"});'>CLICK HERE FOR TEST!!!</a>
</body></html>
服务器端(以CGI为例):
#! /usr/bin/env python
import cgi, cgitb
cgitb.enable()
print "Content-Type: text/html'n"
i = cgi.FieldStorage()
if i.has_key("s"):
if i["s"].value=="bow": print "Yeeeeey!!!! I got 'bow'!!!<br>"
else: print "Woops! I got", i["s"].value, "instead of 'bow'<br>"
else: print "Argument 's' not present!<br>"
print "All received arguments:"
for x in i.keys(): print x, "<br>"
相关文章:
- 用beautifulsoup-python调用onclick事件
- 如何在新创建的模块中从Odoo v8中的Javascript文件中调用python函数
- 用javascript调用python函数,并在网页上显示返回的字符串
- 从PHP调用Python脚本
- 只使用javascript或django调用python脚本
- 如何从javaScript调用python或java方法
- 如何从PHP或Javascript调用python程序(具有命令行参数)
- Django,Dajax和Javascript - onclick不调用python方法
- 你能用JavaScript函数调用Python函数吗
- 如何在Odoo 10中每次调用python函数
- 从JS调用python函数
- 在MAMP中使用Javascript调用Python函数
- 如何从Node.js调用Python函数?
- 使用JQuery调用python脚本
- 如何从HTML页面调用python命令
- 如果多个用户在同一时刻调用Python函数,它是否工作?
- 如何从javascript post调用python脚本读取数据
- 如何在点击按钮上调用python代码中的函数-来自javascript onClick调用python
- 如何像在Javascript中那样调用Python函数
- 在Python应用程序中使用WebKitGtk+时,是否有一种方法可以从JS调用Python函数?