在Python CGI中打印HTML [英] Printing HTML in Python CGI

查看:194
本文介绍了在Python CGI中打印HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在教自己python和cgi脚本,并且我知道你的基本脚本看起来像

 #!/ usr / local / bin / python 
import cgi
printContent-type:text / html
print
print< HTML>
打印< BODY>
打印HELLO WORLD!
打印< / BODY>
打印< / HTML>

我的问题是,如果我有一个大的HTML文件,我想用python显示和代码行和sone JS)我必须在每行前面手动添加'print'并将其转换为\等等吗?或者是否有方法或脚本可以将其转换为我?

谢谢!

解决方案<

  print''Python支持多行字符串,因此您可以在一个大的文件中打印出您的文本。 < HTML> 
< head>< title>我的第一个Python CGI应用< / title>< / head>
< body>
< p> Hello,'world'!< / p>
< / body>
< / html>'''

它们支持所有字符串操作,包括方法 .upper() .translate()等)和格式()以及原始模式( r 前缀)和 u unicode前缀。 / p>

I've been teaching myself python and cgi scripting, and I know that your basic script looks like

#!/usr/local/bin/python
import cgi
print "Content-type: text/html"
print 
print "<HTML>"
print  "<BODY>"
print "HELLO WORLD!"
print "</BODY>"
print "</HTML>"

My question is, if I have a big HTML file I want to display in python (it had lines and lines of code and sone JS in it) do I have to manually add 'print' in front of each line and turn "s into \" , etc? Or is there a method or script that could convert it for me?

Thanks!

解决方案

Python supports multiline strings, so you can print out your text in one big blurb.

print '''<html>
<head><title>My first Python CGI app</title></head>
<body>
<p>Hello, 'world'!</p>
</body>
</html>'''

They support all string operations, including methods (.upper(), .translate(), etc.) and formatting (%), as well as raw mode (r prefix) and the u unicode prefix.

这篇关于在Python CGI中打印HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆