#!/usr/bin/python
print "This is python."
print "<script type="text/javascript">
var pass_to_python = new Number(7)
</script>"
the_number = pass_to_python???
我如何在Python中pass_to_python?
#!/usr/bin/python
print "This is python."
print "<script type="text/javascript">
var pass_to_python = new Number(7)
</script>"
the_number = pass_to_python???
我如何在Python中pass_to_python?
您可以将GET
或POST
添加到Python脚本中。如果您需要动态执行此操作,则可以使用AJAX。
这里是一个很好的链接:How are POST and GET variables handled in Python?
HTTP是一个简单的请求 - 响应协议,它不会让你暂停中流,等待来自客户端的详细信息 - 而且由于你的JS在运行浏览器(JS可以在服务器上运行,但如果大多数人不需要代码在浏览器中运行,大多数人都不会尝试这种方式,所以我假设使用服务器端JS是不可能的) Python在服务器上运行,这就是你的代码工作所需要的东西(以及修复你在Python代码中嵌套的断言)。
您需要加载完整文档,然后发出新的HTTP请求。
这可能涉及具有JS设置location.href
(making sure you have a fallback for non-JS clients),它可能涉及到使用XMLHttpRequest的异步加载新的数据,它可能采用另一种技术是最好的(这是很难说肯定是你的榜样简化太多to tell what X is)
使用pyv8你可以在Python中执行javascript。
import PyV8
class Global(PyV8.JSClass):
pass
with PyV8.JSContext(Global()) as ctxt:
the_number = ctxt.eval("var pass_to_python = new Number(7)")
完美!这个导入模块和行代码... – opmeitle 2012-11-03 04:26:02
对不起,我不明白你在说什么。 – 2012-11-05 00:27:09
我所看到的一种奉承形式,仅此而已。 – opmeitle 2012-11-05 03:26:00