如何从Python CGI脚本返回值

时间:2018-10-29 07:22:06

标签: javascript python html python-3.x cgi

我正在用python编写CGI脚本,以哈希JSON文件的内容。而且我必须在HTML页面中获取哈希值。散列JSON文件内容已成功执行。但是我坚持返回哈希值。

CGI:

#!/usr/bin/python
import json
import cgi, os
import cgitb; cgitb.enable()
from hashlib import sha256
from collections import OrderedDict

form = cgi.FieldStorage()

# Get filename here.
fileitem = form['filename']
content = fileitem.value
cert_json = json.loads(content, object_pairs_hook=OrderedDict)
det = json.dumps(cert_json)
hashed = sha256(det.encode('utf8')).hexdigest()
# Test if the file was uploaded
if fileitem.filename:

   fn = os.path.basename(fileitem.filename)
   open('/tmp/' + fn, 'wb').write(fileitem.file.read())

   message = 'The file "' + fn + '" was uploaded successfully'

else:
   message = 'No file was uploaded'

print """\
Content-Type: text/html\n
<html>
<body>
   <p>%s</p>
   <p>hash:%s<p>
</body>
</html>
""" % (message,hashed)

哈希值现在存储在名为“哈希”的变量中。是否可以将此变量传递到另一个页面?

0 个答案:

没有答案