helloworld.py
Tags:

#!/usr/bin/python
# for linux

import sys

def main():
print “Content-type: text/html\n”
print “<HTML><TITLE>Hello World from Python</TITLE><body>”
print “Standard Hello World from a Python CGI Script<p />”

print “It is very important that Python CGI scripts…”
print “<ol>”
print “<li>have ‘<code>!#usr/bin/python</code>’ ”
print “as the first line of the file<br/>”
print “By default, this will get you Python %s<br/>” % sys.version
print “For specific versions of Python here at Dreamhost…<br/>”
print “v2.1.3 -> <code>#!/usr/bin/python2.1</code><br/>”
print “v2.2.1 -> <code>#!/usr/bin/python2.2</code><br/>”
print “v2.3.5 -> <code>#!/usr/bin/python2.3</code><br/>”
print “</li>”
print “<li>end in ‘<code>.py</code>’</li>”
print “<li>print the ‘<code>Content-type: text/html\\n</code>’ header”
print ” in the first line if you want to view the output</li>”
print “<li>mark the file as executable: ”
print “‘<code>chmod 755 [filename.py]</code>’</li>”
print “</ol>”
print “<hr />”
print “This is a self printing file, full source code follows<br/>”
print ‘Or you could view the source directly ‘
print ‘<a href=”helloworld.txt”>helloworld.txt</a>’
print “<hr />”
f = open(’helloworld.py’)
buff = f.readlines()
print “<pre>”
for line in buff:
# remove trailing newline
line = line.rstrip()
# escape less than/greater thans from &lt; &gt; to &amp;lt; &amp;gt; so they are viewable in html
# side effect: in a web browser it will look like it’s doing nothing ;-)
line = line.replace(”<”,”&lt;”)
line = line.replace(”>”,”&gt;”)
print line
print “</pre>”
print “</body></HTML>”

if( __name__ == “__main__”):
try:
main()
except:
print “Content-type: text/html\n”
print “<HTML>”
print “<title>error?</title>”
print “<body>”
print “There was an error on the page<p/>”
print “Notes:<br/>”
print “&nbsp;exception handling is your friend</br>”
print “&nbsp;cgitb only works with current versions (>=2.3 ?) of Python<br/>”
print “&nbsp;not even an except block can recover from a print ”
print “without a closing quote - you’ll get an error 500 from the server”
print “<body>”
print “</HTML>”

#EndOfFile

AddThis Social Bookmark Button

相关文档(Relevant Entries)
实现URL编码解码的python程序
python资源
使用python为Java项目增加一个预编译脚本[转]
Python Web frameworks, Part 1
Python Web frameworks, Part 2
Plone是什么?
Open Source GIS-Python Laboratory(基于Python的开源GIS项目推荐)
Python连接MySQL
WoW Powerleveling
Posted on October 19, 2007 5:38 PM | | | Comments (0) | | TrackBacks (0)

引用地址(TRACKBACKS)
 
TrackBack URL for this entry:
http://www.wujianrong.com/mt-tb.cgi/6341

发布评论(ADD YOUR COMMENTS)
 
感谢您参与评论;发表您的意见时请保持文章的相关性;不相关的或是单纯宣传的内容可能会被删掉。您的E-mail只是用来确认您发表的文章,不会出现在网页上。
Please keep your comments relevant to this blog entry. Email addresses are never displayed, but they are required to confirm your comments.

称呼(Name):      记住我的个人信息(Remember)
邮箱(Email):
网址(URL):
评论(Add your comments):

相关内容
广告计划