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

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

问题描述

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

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>"

我的问题是,如果我想在 python 中显示一个大的 HTML 文件(它有几行和几行代码,里面有 sone JS),我是否必须在每行前面手动添加打印"并打开"s into " 等?或者有没有可以帮我转换的方法或脚本?

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?

谢谢!

推荐答案

Python 支持多行字符串,因此您可以在一个大简介中打印出您的文本.

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>'''

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

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天全站免登陆