问题
I would like to display the result of a CGI script on the original HTML page. The best way is to trigger the CGI process with a click on a button but not reload the page.
I read this about Ajax, but how can I integrate the result of the CGI script into the HTML page?
#!/usr/bin/python2.7
# -*- encoding: utf-8 -*-
import cgi, cgitb
import time
import os
import mmap
form = cgi.FieldStorage()
password = form.getvalue('passwordcheck')
inputformular = form.getvalue('myfield')
x=inputformular
print ("""Content-Type:text/html\n
<!DOCTYPE html>
<html>
<head>
<title>Passwordcheck</title>
</head>
<body>
""")
start = time.time()
##################################
FIELD_SIZE=40+1 # Also include a newline separator
def binarySearch(mm, l, r, x):
while l <= r:
mid = int(l + (r - l)/2);
# Check if x is present at mid
mid_slice = mm[mid*FIELD_SIZE:(mid+1)*FIELD_SIZE]
mid_slice = mid_slice.decode('utf-8').strip()
#print(mid_slice)
if mid_slice == x:
return mid
# If x is greater, ignore left half
elif mid_slice < x:
l = mid + 1
#print l
# If x is smaller, ignore right half
else:
r = mid - 1
#print r
# If we reach here, then the element
# was not present
return -1
# text=f.readlines()
# data=text
#print data
#x = 'C78A6A8BFA18BABB7762CEF6AEFA14EE299F10AF'
#x = '0000000CAEF405439D57847A8657218C618160B2'
with open('../stripedpwned.txt', 'r+b') as f:
mm = mmap.mmap(f.fileno(), 0)
f.seek(0, os.SEEK_END)
size = f.tell()
result = binarySearch(mm, 0, size/FIELD_SIZE, x)
if result != -1:
print("Element is present at index % d" % result)
else:
print("Element is not present in array")
###################################
print("<br />")
print("Uebermittelte Daten:<br />")
print("Password:")
print(password)
print("<br />")
print("Der Hash davon:")
print(x)
print("<br />")
end = time.time()
print(end - start)
print("""
</body>
</html>
""")
I would like to read the variable "result" in the HTML page without getting directed to the CGI script.
EDIT:
I have the following JavaScript code in my HTML File:
function loadCGI(){
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', '../cgi-bin/einlesen.py', false);
httpRequest.send(null);
document.write(httpRequest.responseText);
}
So I could read a variable, which I output via the CGI script. E.g. Value=true or something like that. But how do I manage that first the FormInput of my HTML page is transferred into the CGI script and then I get the result? I could then read this somehow as a JavaScript variable.
来源:https://stackoverflow.com/questions/58164870/how-do-i-get-it-to-display-the-result-on-the-original-html-page-after-executing