在 Python CGI 中打印 HTML

发布于 2024-09-07 21:10:12 字数 383 浏览 6 评论 0原文

我一直在自学 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) )我是否必须在每行前面手动添加“print”并将“s 转换为 \”等?或者有没有一种方法或脚本可以为我转换它?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不弃不离 2024-09-14 21:10:12

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

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

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

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.

悸初 2024-09-14 21:10:12

那个大 html 文件被称为(例如)'foo.html' 并位于 CGI 脚本的当前目录中,那么您所需的脚本主体就是:

print "Content-type: text/html"
print
with open('foo.html') as f:
  print f.read()

如果 Python 2.5,添加 from __future__ import with_statement 作为模块主体的开头。如果您坚持使用更旧的 Python,请将最后两行更改为

print open('foo.html').read()

注意,当您不使用 cgi< 的任何功能时,您不需要 import cgi /code> 模块,您的示例和本答案中都是这种情况。

If that big html file is called (for example) 'foo.html' and lives in the current directory for your CGI script, then all you need as your script's body is:

print "Content-type: text/html"
print
with open('foo.html') as f:
  print f.read()

If you're stuck with Python 2.5, add from __future__ import with_statement as the start of your module's body. If you're stuck with an even older Python, change the last two lines into

print open('foo.html').read()

Note that you don't need to import cgi when you're using none of the functionality of the cgi module, which is the case both in your example and in this answer.

吾性傲以野 2024-09-14 21:10:12

当我第一次尝试装饰器时,我编写了这个小CGI装饰器来处理HTML头部和身体标记样板内容。这样您就可以编写:

@CGImethod(title="Hello with Decorator")
def say_hello():
    print '<h1>Hello from CGI-Land</h1>'

调用时返回:

Content-Type: text/html

<HTML>
<HEAD><TITLE>Hello with Decorator</TITLE></HEAD>
<BODY>
<h1>Hello from CGI-Land</h1>

</BODY></HTML>

然后可以从 HTTP 服务器的 do_GETdo_POST 方法调用 say_hello

When I was first experimenting with decorators, I wrote this little CGI decorator to handle the HTML head and body tag boilerplate stuff. So that you can just write:

@CGImethod(title="Hello with Decorator")
def say_hello():
    print '<h1>Hello from CGI-Land</h1>'

which when called returns:

Content-Type: text/html

<HTML>
<HEAD><TITLE>Hello with Decorator</TITLE></HEAD>
<BODY>
<h1>Hello from CGI-Land</h1>

</BODY></HTML>

Then say_hello could be called from your HTTP server's do_GET or do_POST methods.

捎一片雪花 2024-09-14 21:10:12

Python 支持多行字符串。因此,您只需复制 HTML 代码并将其粘贴到引用中即可。

print ("<html>
<head>
<title>
</title>
</head>
</html>")

等等!

Python supports multiline string. So you can just copy your HTML code and paste it into the quotations.

print ("<html>
<head>
<title>
</title>
</head>
</html>")

and so on!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文