2014-05-05 46 views
0

我想要在终端中使用HTML格式化一个.py文件(在每次刷新页面时都会生成一个随机面部),该文件可以在浏览器中运行。我有chmod`ed它所以它应该工作,但每当我在浏览器中运行它,我得到一个内部服务错误。有人能帮我弄清楚它有什么不对吗?使用HTML格式化.py文件

#!/usr/bin/python 
print "Content-Type: text/html\n" 
print "" 

<!DOCTYPE html> 
<html> 
<pre> 

from random import choice 

def facegenerator(): 
    T = "" 
    hair = ["I I I I I","^^^^^"] 
    eyes = ["O O"," O O ",] 
    nose = [" O "," v "] 
    mouth = ["~~~~~","_____","-----"] 
    T += choice(hair) 
    T += "\n" 
    T += choice(eyes) 
    T += "\n" 
    T += choice(nose) 
    T += "\n" 
    T += choice(mouth) 
    T += "\n" 
    return T 

print facegenerator() 
</pre> 
</html> 

该代码在IDLE中工作,但我无法在网页上工作。预先感谢任何帮助!

回答

1

这既不是有效的HTML,也不是有效的Python。您不能简单地将HTML标签混合到Python脚本的中间,至少需要将它们放在引号内,以便它们是有效的字符串。

#!/usr/bin/python 
print "Content-Type: text/html\n" 
print """ 

<!DOCTYPE html> 
<html> 
<pre> 
""" 
def ... 

print facegenerator() 
print """</pre> 
</html>"""