如何使用 javascript 从 cgi 脚本调用 python 函数?
How do I call a python function from a cgi script with javascript?
我还想在 javascript 函数中使用 XMLHttpRequest 对象作为参数从下拉列表(下面代码中称为 'Users' 的选项)发送有关当前所选选项的信息到我的 Python 脚本中的一个函数,然后从该函数中获取 return 值并使用它来设置另一个列表(下面代码中的 'Tasks' )我想这样做以便我可以使用从 Python 函数中作为 return 值获得的数据更新 "Tasks" 中的信息 我这样做是因为我需要页面不必重新加载。如果您知道更好的实现方法,我愿意接受您的想法。
def main():
with open("Users.json", 'r') as data_file:
usersDictionary = json.load(data_file)
Users=usersDictionary["Users"]
print "Content-Type: text/html"
print ""
print """
<html>
<head>
<script>
function setTaskList(){
#Insert Javascript which will call python function here...
}
</script>
<title>TestPage</title>
</head>
<body onload="setTaskList()">
<form>"""
print """<select name="Users" onchange="setTaskList()">"""
for user in Users:
if(not(len(user["Tasks"]["High"])==0 and len(user["Tasks"]["Med"])==0 and len(user["Tasks"]["Low"])==0)):
print """<option value="{0}">{1}</option>""".format(user["UID"], user["name"])
print "</select>"
print """<select name="Tasks" disabled="disabled">"""
print "</select>"
print"""<input type="Submit" value="Add Task"/>
<button type="button">Delete Task</button>"""
print"""
</form>
</body>
</html>"""
main()
对于下面的代码,我希望能够在单击提交时从输入框中获取数据,并将从输入框和单选按钮中获取的信息发送到 python 函数进行处理并将其作为 JSON 对象添加到 JSON 字典,然后更新 JSON 文件,然后 return 到上一页(即上面代码的那一页) )(假设称为 index.py)。
print "Content-Type: text/html"
print ""
print"""<html>
<head>
<title>Title</title>
</head>
<body>
<form action = "/~theUser/cgi-bin/index.cgi" method = "POST">
Task Name: <input type = "text" name = "TaskName"
placeholder="Task name" /> <br />
Task Description: <input type = "text" name = "TaskDescription"
placeholder="task description" /> <br />
User ID: <input type = "text" name = "UID" placeholder="User Id"
/> <br />
Priority <br/>
<input type="radio" name="gender" value="High"> High<br>
<input type="radio" name="gender" value="Medium"> Medium<br>
<input type="radio" name="gender" value="Low"> Low<br>
<input type = "submit" value = "Submit" />
</form>
</body>
</html>"""
任何人都可以帮忙吗,我对这个 CGI 东西真的很陌生,真的很感激。另外,如果您知道我可以更好地执行此操作,请告诉我。
谢谢!
所以经过长时间的尝试和错误,并等待一个从未出现的答案,我想出了如何自己做这个所以我想我可以帮助任何需要这个的人我能够发送请求来自我的 python cgi 脚本使用 java 脚本,如下所示:
print "Content-Type: text/html"
print ""
print """
<html>
<head>
<script>
function getTasks() {
//put more processing in the function as needed
var xmlhttp;
var parameters = "This must be a string which will be the parameters
you will receive in your python script";
var scriptName = "pythonScript To CommunicateWith.py";
//may be .cgi as well depending on how you are using it
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST", scriptName, true);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//retrieve a json response and parse it into userTasks
usersTasks = JSON.parse(xmlhttp.responseText);
}
}
xmlhttp.send(parameters);
}
</script>
在我的 python 脚本中,java 脚本点击这里是我获取参数并处理它们的方式:
#!usr/bin/python
import sys
import json
args=sys.stdin.readlines() #args comes in as a list with one item in it
which is the parameter that you sent in from javascript
arguments=args[0]
print "Content-Type: text/html"
print ""
def getTasks(userName):
"""This function will do some processing and get the required return
value"""
taskList=[]
#read JSON file and get the info.
with open("Somefile.json", 'r') as data_file:
usersDictionary = json.load(data_file)
data_file.close()
"""...do some process ing of data here, which will set some data into
the list called taskList"""
return taskList #Return a list of all the tasks for the user.
print json.dumps(getTasks(arguments))
"""convert the json to a string and print it out. what you print out here
will be what you will get as a string in the .responseText of the
XMLHttpRequest
object"""
使用 Pycharm 和 python CGIHTTPServer 函数调试对我有帮助。
希望这可以帮助那里的人。
我还想在 javascript 函数中使用 XMLHttpRequest 对象作为参数从下拉列表(下面代码中称为 'Users' 的选项)发送有关当前所选选项的信息到我的 Python 脚本中的一个函数,然后从该函数中获取 return 值并使用它来设置另一个列表(下面代码中的 'Tasks' )我想这样做以便我可以使用从 Python 函数中作为 return 值获得的数据更新 "Tasks" 中的信息 我这样做是因为我需要页面不必重新加载。如果您知道更好的实现方法,我愿意接受您的想法。
def main():
with open("Users.json", 'r') as data_file:
usersDictionary = json.load(data_file)
Users=usersDictionary["Users"]
print "Content-Type: text/html"
print ""
print """
<html>
<head>
<script>
function setTaskList(){
#Insert Javascript which will call python function here...
}
</script>
<title>TestPage</title>
</head>
<body onload="setTaskList()">
<form>"""
print """<select name="Users" onchange="setTaskList()">"""
for user in Users:
if(not(len(user["Tasks"]["High"])==0 and len(user["Tasks"]["Med"])==0 and len(user["Tasks"]["Low"])==0)):
print """<option value="{0}">{1}</option>""".format(user["UID"], user["name"])
print "</select>"
print """<select name="Tasks" disabled="disabled">"""
print "</select>"
print"""<input type="Submit" value="Add Task"/>
<button type="button">Delete Task</button>"""
print"""
</form>
</body>
</html>"""
main()
对于下面的代码,我希望能够在单击提交时从输入框中获取数据,并将从输入框和单选按钮中获取的信息发送到 python 函数进行处理并将其作为 JSON 对象添加到 JSON 字典,然后更新 JSON 文件,然后 return 到上一页(即上面代码的那一页) )(假设称为 index.py)。
print "Content-Type: text/html"
print ""
print"""<html>
<head>
<title>Title</title>
</head>
<body>
<form action = "/~theUser/cgi-bin/index.cgi" method = "POST">
Task Name: <input type = "text" name = "TaskName"
placeholder="Task name" /> <br />
Task Description: <input type = "text" name = "TaskDescription"
placeholder="task description" /> <br />
User ID: <input type = "text" name = "UID" placeholder="User Id"
/> <br />
Priority <br/>
<input type="radio" name="gender" value="High"> High<br>
<input type="radio" name="gender" value="Medium"> Medium<br>
<input type="radio" name="gender" value="Low"> Low<br>
<input type = "submit" value = "Submit" />
</form>
</body>
</html>"""
任何人都可以帮忙吗,我对这个 CGI 东西真的很陌生,真的很感激。另外,如果您知道我可以更好地执行此操作,请告诉我。 谢谢!
所以经过长时间的尝试和错误,并等待一个从未出现的答案,我想出了如何自己做这个所以我想我可以帮助任何需要这个的人我能够发送请求来自我的 python cgi 脚本使用 java 脚本,如下所示:
print "Content-Type: text/html"
print ""
print """
<html>
<head>
<script>
function getTasks() {
//put more processing in the function as needed
var xmlhttp;
var parameters = "This must be a string which will be the parameters
you will receive in your python script";
var scriptName = "pythonScript To CommunicateWith.py";
//may be .cgi as well depending on how you are using it
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST", scriptName, true);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//retrieve a json response and parse it into userTasks
usersTasks = JSON.parse(xmlhttp.responseText);
}
}
xmlhttp.send(parameters);
}
</script>
在我的 python 脚本中,java 脚本点击这里是我获取参数并处理它们的方式:
#!usr/bin/python
import sys
import json
args=sys.stdin.readlines() #args comes in as a list with one item in it
which is the parameter that you sent in from javascript
arguments=args[0]
print "Content-Type: text/html"
print ""
def getTasks(userName):
"""This function will do some processing and get the required return
value"""
taskList=[]
#read JSON file and get the info.
with open("Somefile.json", 'r') as data_file:
usersDictionary = json.load(data_file)
data_file.close()
"""...do some process ing of data here, which will set some data into
the list called taskList"""
return taskList #Return a list of all the tasks for the user.
print json.dumps(getTasks(arguments))
"""convert the json to a string and print it out. what you print out here
will be what you will get as a string in the .responseText of the
XMLHttpRequest
object"""
使用 Pycharm 和 python CGIHTTPServer 函数调试对我有帮助。 希望这可以帮助那里的人。