如何用Python显示网页?

0 投票
6 回答
8049 浏览
提问于 2025-04-17 10:36

我在大学学习Python。 我有一个很傻的问题。 我该如何用Python制作一个网页呢?

我这样做并保存为test.py

print "Hello test \n";
print "\n"
print "Just testing \n";

当我访问这个网址时,我得到的输出是一样的

print "Hello test \n";
print "\n"
print "Just testing \n";

我真的很困惑。我到底做错了什么?我的网站托管在HostGator上。在他们的网站上说要这样做:

#!/usr/bin/python
print "Content-type: text/html\n\n";
print "<html><head>";
print "<title>CGI Test</title>";
print "</head><body>";
print "<p>Test page using Python</p>";
print "</body></html>";

这样做也给我同样的错误。

6 个回答

1

如果你想学习,可以试试:cherrypy 或者 werkzeug。这两个都是简单的网页框架。

2

先在你自己的电脑上试试这个:用Python制作一个简单的网络服务器。

0

好的,这个问题解决得很好。

之前遇到了一些麻烦。

1- 我在使用HostGator的时候遇到的问题是:

Windows and Linux use different line endings for text files. Windows uses the CR and LF characters to terminate a line, but Linux just uses the LF character. This can lead to problems when you write a script on Windows and then try to execute it directly on Linux. 

2- 我需要在我的.htaccess文件中添加

AddHandler cgi-script .py

我在Notepad++中再次编辑,添加了htaccess文件,然后就可以用了。

我在本地遇到的问题也解决了。

我把Apache放在了U盘上。

我需要做的就是:

1- 在Apache中添加这个:

Options Indexes FollowSymLinks ExecCGI

2- 在这一行的末尾添加.py

AddHandler cgi-script .cgi

3- 在我的oy文件开头添加Python的路径

#!E:/EasyPHP-5.3.8.1/Python27/python.exe -u

撰写回答