Javascript 如何在客户机-服务器Web交互期间执行Python代码
我目前正在使用Javaservlet和Python编写一个客户机/服务器Web应用程序。尽管我在客户端交互方面遇到了问题,主要是在生成输入方面。生成输入需要通过在客户端本地执行python脚本来修改用户输入的数据,然后通过POST请求提交给Servlet服务器。在此之后,Servlet将把接收到的数据传递给另一个python脚本服务器端,以验证给定输入的有效性。Python在这个过程中是必要的,因为计算量很大(2^4096)位长 所以我真正想问的是Javascript 如何在客户机-服务器Web交互期间执行Python代码,javascript,java,python,cryptography,servlet-3.0,Javascript,Java,Python,Cryptography,Servlet 3.0,我目前正在使用Javaservlet和Python编写一个客户机/服务器Web应用程序。尽管我在客户端交互方面遇到了问题,主要是在生成输入方面。生成输入需要通过在客户端本地执行python脚本来修改用户输入的数据,然后通过POST请求提交给Servlet服务器。在此之后,Servlet将把接收到的数据传递给另一个python脚本服务器端,以验证给定输入的有效性。Python在这个过程中是必要的,因为计算量很大(2^4096)位长 所以我真正想问的是 当用户使用表单访问网页,填写表单,然后单击提交
当用户使用表单访问网页,填写表单,然后单击提交按钮时,如何从表单字段中提取数据,将内容作为输入参数传递到本地python脚本中,执行python脚本。然后将新的计算数据从python脚本返回到web浏览器,以便向服务器发送POST请求。这里有两种可能性: 1)Java作为控制器 从Java Servlet中获取表单数据:
String Form_text_data = request.getParameter("text_input");
如果有许多字段,您可以实例化一个StringBuilder(包含您需要的所有请求字段),以获得所有目标字段答案中的一个字符串-请确保在将这些字段添加到字符串生成器之前对其进行健全性检查
然后将字符串结果从字符串生成器传递给具有exec方法的运行时类:
StringBuilder Python_script_command = new StringBuilder("python PATH/TO/YOUR/PYTHON_SCRIPT.py");
Python_script_command.append(Form_text_data) // Repeat this as many time as you wish for each form fields, but be sure to make sanity check before to avoid any injections that you do not want
Process p = Runtime.getRuntime().exec(Python_script_command.toString());
然后,您可以通过以下方式绑定输出:
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
有关更多详细信息,请参阅本文:
2)从python API框架(如FLASK)执行
您还可以直接在python中构建RESTAPI,当用户提交表单时,您可以通过POST请求将表单发送到python应用程序
然后,提取您需要的数据非常简单:
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
default_name = '0'
data = request.form.get('input_name', default_name) #here is an example on one data getter from a form field
有关更多详细信息,请参见此答案()
然后直接在python中使用此数据,再次进行各种健全性检查这里有两种可能性: 1)Java作为控制器 从Java Servlet中获取表单数据:
String Form_text_data = request.getParameter("text_input");
如果有许多字段,您可以实例化一个StringBuilder(包含您需要的所有请求字段),以获得所有目标字段答案中的一个字符串-请确保在将这些字段添加到字符串生成器之前对其进行健全性检查
然后将字符串结果从字符串生成器传递给具有exec方法的运行时类:
StringBuilder Python_script_command = new StringBuilder("python PATH/TO/YOUR/PYTHON_SCRIPT.py");
Python_script_command.append(Form_text_data) // Repeat this as many time as you wish for each form fields, but be sure to make sanity check before to avoid any injections that you do not want
Process p = Runtime.getRuntime().exec(Python_script_command.toString());
然后,您可以通过以下方式绑定输出:
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
有关更多详细信息,请参阅本文:
2)从python API框架(如FLASK)执行
您还可以直接在python中构建RESTAPI,当用户提交表单时,您可以通过POST请求将表单发送到python应用程序
然后,提取您需要的数据非常简单:
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
default_name = '0'
data = request.form.get('input_name', default_name) #here is an example on one data getter from a form field
有关更多详细信息,请参见此答案()
然后在python中直接使用这些数据,再次进行各种健全性检查