2013-12-08 81 views
0

我在编写一个简单的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,但我需要该脚本。

我该如何执行脚本而不出错?

+0

你觉得'f.format(x = val1,y = val2)'是做什么的? – SLaks

+0

它在输入框“val1”处分配字符串“x”。 y和val2也是一样。 val1和val2在HTML正文中声明!这只是一个测试,但我如何执行JavaScript? –

+0

确保Javascript看起来不像破损的格式字符串,因为错误告诉你。另外,你有一个XSS洞。 – SLaks

回答

1

我认为问题在于格式期望两个法语大括号之间的任何内容都被您的一个格式字符串替换。在你的情况,然后,它试图查找

$("#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"); 

作为您通过的kwargs中的关键。这里概述的解决方案String format a JSON string gives KeyError是使用双括号。然后,您的新的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> 

(注意从变化 '{' 到 '{{' 和 '}' 到 '}}'。)

让我知道如果你有任何后续问题/一些不起作用。

+0

它工作!谢谢! :d –