javascript 将表单数据传递给python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14818157/
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
Passing form data to python
提问by BloonsTowerDefence
I have an HTML form with data that I would like to send to my SVN. Since HTML/JS have no means of doing this, I want to use Python as a link between the form and the SVN. My problem is that I do not know how to send data from HTML/JS to Python, both of which are client side (there is no server involved).
我有一个 HTML 表单,其中包含我想发送到我的 SVN 的数据。由于 HTML/JS 没有办法做到这一点,我想用 Python 作为表单和 SVN 之间的链接。我的问题是我不知道如何将数据从 HTML/JS 发送到 Python,两者都是客户端(不涉及服务器)。
What I imagined would happen is that a user would fill out the form, then press a 'submit' button, which would call a Python script and pass their form data as arguements.
我想象会发生的是,用户会填写表单,然后按下“提交”按钮,这将调用 Python 脚本并将他们的表单数据作为参数传递。
I have been searching and found that people are running Python server side and POSTing to it from their javascript, but since I have no server I don't think this is possible for me.
我一直在搜索并发现人们正在运行 Python 服务器端并从他们的 javascript 发布到它,但由于我没有服务器,我认为这对我来说是不可能的。
Is it possible to send data from HTML/JS to Python if they are both client side?
如果它们都是客户端,是否可以将数据从 HTML/JS 发送到 Python?
EDIT: I should add that I do have a good background in Python and JS
编辑:我应该补充一点,我确实有很好的 Python 和 JS 背景
回答by Torxed
Here's a few neat ways of combining Python with JavaScript:
这里有一些将 Python 与 JavaScript 结合的巧妙方法:
Return data from html/js to python
Note:Since you mentioned you had no server, the request you call with the javascript has to be pointed to the listening port of the socket that the python code runs on. Easy enouhg would be to listen on port 80 with python and just do regular calls without thinking twice to the :80 from JavaScript.
注意:由于您提到您没有服务器,因此您使用 javascript 调用的请求必须指向运行 python 代码的套接字的侦听端口。简单的方法是使用 python 侦听端口 80 并且只进行常规调用,而不用考虑来自 JavaScript 的 :80。
Basically, HTML form, use JavaScript onSubmit()
or a buttonthat calls the AJAX
code in the post above, then have Python read the JSON
data (structure the <form>
data according to the JSON format
shown at the top of the link)
基本上,HTML 表单,使用 JavaScriptonSubmit()
或调用上面帖子中的代码的按钮AJAX
,然后让 Python 读取JSON
数据(<form>
根据JSON format
链接顶部显示的结构构建数据)
Here's a short intro on how to use form data via javascript:
下面是关于如何通过 javascript 使用表单数据的简短介绍:
<HTML>
<HEAD>
<TITLE>Test Input</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function testResults (form) {
var TestVar = form.inputbox.value;
alert ("You typed: " + TestVar);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="myform" ACTION="" METHOD="GET">Enter something in the box: <BR>
<INPUT TYPE="text" NAME="inputbox" VALUE=""><P>
<INPUT TYPE="button" NAME="button" Value="Click" onClick="testResults(this.form)">
</FORM>
</BODY>
</HTML>
Use this principle to gather your information,
then build in the AJAX part in the link mentioned at the top..
Once you've done that, start a python script (shown in the link as well) that listens for these calls.
使用此原则收集您的信息,
然后在顶部提到的链接中构建 AJAX 部分。
完成此操作后,启动侦听这些调用的 python 脚本(也显示在链接中)。
Remember:To use JSON, format it properly, '
will not be allowed for instance, it has to be "
!
请记住:要使用JSON,请正确格式化'
,例如不允许,必须是"
!
In my link, this is the important part that sends the GET request to the "server" (python script):
在我的链接中,这是将 GET 请求发送到“服务器”(python 脚本)的重要部分:
xmlhttp.open("GET","Form-data",true);
Here's the python part:
这是python部分:
from socket import *
import json
s = socket()
s.bind(('', 80)) # <-- Since the GET request will be sent to port 80 most likely
s.listen(4)
ns, na = s.accept()
while 1:
try:
data = ns.recv(8192) # <-- Get the browser data
except:
ns.close()
s.close()
break
## ---------- NOTE ------------ ##
## "data" by default contains a bunch of HTTP headers
## You need to get rid of those and parse the HTML data,
## the best way is to either just "print data" and see
## what it contains, or just try to find a HTTP parser lib (server side)
data = json.loads(data)
print data