我一直在自学 python 和 cgi 脚本,我知道你的基本脚本看起来像
#!/usr/local/bin/python
import cgi
print "Content-type: text/html"
print
print "<HTML>"
print "<BODY>"
print "HELLO WORLD!"
print "</BODY>"
print "</HTML>"
我的问题是,如果我有一个大的 HTML 文件,我想在 python 中显示(它有一行又一行的代码和一个 JS),我是否必须在每一行前面手动添加“打印”并打开"s into\"等?或者是否有可以为我转换它的方法或脚本?
谢谢!
最佳答案
Python 支持多行字符串,因此您可以在一个大的 blurb 中打印出您的文本。
print '''<html>
<head><title>My first Python CGI app</title></head>
<body>
<p>Hello, 'world'!</p>
</body>
</html>'''
它们支持所有的字符串操作,包括方法(.upper()
、.translate()
等)和格式化(%
),以及原始模式(r
前缀)和 u
unicode 前缀。
关于python - 在 Python CGI 中打印 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3169781/