【问题标题】:how to call python command from html page?如何从html页面调用python命令?
【发布时间】:2015-10-17 15:18:41
【问题描述】:

我想从我的 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    

这里的弓是从输入框得到的。

【问题讨论】:

  • 我认为这不可能是客户端(即浏览器),但服务器端.. django、flask 等可以用于(即基于执行 python 代码)来自网页的参数)
  • 您需要使用网络服务器来处理来自您网页的传入请求。

标签: javascript jquery python html


【解决方案1】:

使用 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>"

【讨论】:

  • 什么是 argdict ?和服务器端我需要添加新文件或编辑预先存在的文件?
  • argdict 是关联数组,用于保存查询字符串的参数,这些参数将传递给您的服务器端 python 脚本。它将使用 POST 方法自动转换和传递。我称它为 argdict 是因为它的定义与 Python 中的相同,并且具有相同的目的。看看我是如何在 onclick 事件中调用 callpy() 函数的。
  • 服务器端 Python 代码必须由 Web 服务器执行。可以以我在回答中提到的任何方式执行执行。我提供的示例服务器端脚本是一个 CGI 脚本。您可以改用 WSGI,如果您愿意,可以使用 django 或 flask 或任何您想要的。重要的是,您的脚本必须从网站接收您的参数,并产生所需的输出。
  • 您可以将我的客户端 JavaScript 代码复制粘贴到您的 HTML 中,添加您需要的内容而不是 'document.write(data)',然后在需要的地方调用 callpy() 函数。您还可以修改我的 CGI 脚本来执行您需要的处理,并在您的 Web 服务器中启用 CGI 来执行它。除此之外,我的代码显示了它是如何完成的,而不是你应该如何做。这个你必须自己解决,因为我不知道你的设置是什么,你到底需要什么。
  • 如果你已经完成了 main.py,你必须在开头添加一个执行行(如果在 Unix 上),添加行 'print "Content-Type: text/html\n', print你的脚本的所有输出到标准输出,使脚本可执行,把它放在正确的目录中,在你的网络服务器中为那个目录启用 CGI,你就完成了 CGI 脚本。
【解决方案2】:

您不能直接从 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/#call-command

【讨论】:

  • ...仅当他在谈论 Django 时。
猜你喜欢
  • 2011-01-30
  • 2012-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
  • 2021-01-11
相关资源
最近更新 更多