Ajax is a good way to pass variables between python and javascript.
Javascript:
param = {a: hello , b: world , c: ! }
$.ajax({
type: "post",
url: "scpi.py",
cache: false,
async: asynchronous ,
dataType: html ,
data: param,
success: function(data) {
console.log(data)
},
error: function(request, status, error){
console.log("Error: " + error)
}
})
Server.py: (You will need a three functions for this to work)
def do_POST(self):
if "scpi.py" in self.path:
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={ REQUEST_METHOD : POST }
)
a = form[ a ].value
b = form[ b ].value
c = form[ c ].value
content = myfunction(a, b, c)
self.respond(content)
def handle_http(self, data):
self.send_response(200)
self.send_header( Content-type , application/json )
self.end_headers()
print(data)
return bytes(str(data), UTF-8 )
def respond(self, data):
response = self.handle_http(data)
print(data)
FYI:“故障(a、b、c、)”是另一个假日文件的功能,然后将数据归还给自己。 回复