如何使用Python中的CGI编程编写第一个Hello World程序?

第一个CGI程序

一个名为hello.py的CGI脚本保存在/ var / www / cgi-bin目录中,并且具有以下内容。在运行CGI程序之前,我们确保使用chmod 755 hello.py UNIX命令更改文件的模式以使文件可执行。

示例

#!/usr/bin/python
print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'

输出结果

如果单击hello.py,则将产生以下输出-

Hello Word! This is my first CGI program