【发布时间】:2014-09-03 00:56:24
【问题描述】:
我正在尝试从 python 脚本获取输出并将其放入我的cherrypy 应用程序的 html 中的表中。
示例应用:
import string, os
import cherrypy
file_path = os.getcwd()
html = """<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>CCMF</title>
<link rel='shortcut icon' type='image/x-icon' href='img/favicon.ico' />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
function b1() {
var request = $.ajax({
url: "b1.py",
type: "POST",
dataType: "text"
});
request.done(function(msg) {
$("#output").html(msg);
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
</script>
</head>
<button onclick="b1()">call b1.py</button>
...
<td id = "output"; style="vertical-align: top; height: 90%; width: 100%;">
<--output goes here -->
</td>
...
</html>
"""
class ccmf(object):
@cherrypy.expose
def index(self):
return html
if __name__ == '__main__':
cherrypy.server.socket_host = "127.0.0.1"
cherrypy.server.socket_port = 8084
config = {
"/img": {
"tools.staticdir.on": True,
"tools.staticdir.dir": os.path.join(file_path, "img"),
}
}
cherrypy.tree.mount(ccmf(), "/", config=config)
cherrypy.engine.start()
cherrypy.engine.block()
这是示例 python 脚本 b1.py:
def b1():
op = "ajax b1 pushed"
print op
return op
b1()
ajax get 被调用但返回失败警报。我试过GET、POST、“text”、“html”,b1.py在同一个目录下,不喜勿喷。所有当前都在我的本地机器上运行。
非常感谢任何提示!
【问题讨论】:
-
我认为你的设计有缺陷。尝试使 b1 成为返回 HTTPResponse 的函数。
-
这也可能是真的,但它似乎也是一个路径问题。如果我在单击按钮时查看 Firefox 中的工具>Web 开发人员>Netwok,我会得到 404。我尝试添加一个 cgi-bin 目录并将 b1.py 放在那里,并添加了“/cgi-bin”:{ "tools.staticdir.on": True, "tools.staticdir.dir": os.path.join(file_path, "cgi-bin") 到配置,但它仍然认为 cwd/cgi-bin/b1.py 没有'不存在。
-
好的,我放弃了cherrypy,正在从apache提供html文件。现在,当我单击按钮时,我得到 b1.py 的内容,而不是运行它并返回正确的输出。一次一步,哈哈。
-
将 apache 配置为运行脚本并返回到我之前的位置 - 404 未找到,尽管它在那里并且所有权限都正确。 :(