我在编写一个简单的python脚本,如果在localhost上启动(使用apache),它将生成一个html页面。生成HTML页面的Python脚本
我的剧本是这样的(test.py):
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cgitb
cgitb.enable()
import cgi
form = cgi.FieldStorage()
print "Content-type: text/html\n\n"
x="hello"
y="world"
f= open('my.html', 'r').read()
print f.format(x=val1, y=val2)
这样就打开了在头元件有一个简单的JavaScript的HTML页:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test html</title>
<script type="text/javascript">
$(document).ready(function() {
$("#select1").change(function() {
var selectedVal = $(this).find("option:selected").val();
$("#select2 option").removeAttr("disabled").removeAttr("selected");
$("#select2 option").each(function() {
if($(this).val() != selectedVal && $(this).val() != -1)
$(this).attr("disabled","disabled").removeAttr("selected");
});
});
});
</script>
</head>
随着在大量的代码身体。 当我运行test.py时,它会显示: Python脚本出现问题。以下是导致错误的函数调用顺序,顺序是它们发生的顺序。
/Library/WebServer/CGI-Executables/test.py in()
181
182
184 f= open('my.html', 'r').read()
=> 185 print f.format(x=val1, y=val2)
f = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN...\n </form>\n <hr>\n </body>\n</html>', f.format = <built-in method format of str object>, val1 = 0, val2 = '','
<type 'exceptions.KeyError'>: '\n\t\t\t $("#select1")'
args = ('\n\t\t\t $("#select1")',)
message = '\n\t\t\t $("#select1")'
但是,如果我删除Javascript python生成没有问题的HTML,但我需要该脚本。
我该如何执行脚本而不出错?
你觉得'f.format(x = val1,y = val2)'是做什么的? – SLaks
它在输入框“val1”处分配字符串“x”。 y和val2也是一样。 val1和val2在HTML正文中声明!这只是一个测试,但我如何执行JavaScript? –
确保Javascript看起来不像破损的格式字符串,因为错误告诉你。另外,你有一个XSS洞。 – SLaks