CGIHTTPServer和Python脚本
我目前正在编写一个python程序,它使用CGIHTTPServer和Python脚本,python,macos,cgi,Python,Macos,Cgi,我目前正在编写一个python程序,它使用sys.stdout.print()打印HTML页面脚本名为hellocgi.py,它位于Mac OS X桌面上的cgi bin目录中 代码如下: import sys sys.stdout.write("Content-type: text/html \r\n\r\n") sys.stdout.write("<!doctype html><html><head><title>Hello World<
sys.stdout.print()
打印HTML页面脚本名为hellocgi.py,它位于Mac OS X桌面上的cgi bin目录中
代码如下:
import sys
sys.stdout.write("Content-type: text/html \r\n\r\n")
sys.stdout.write("<!doctype html><html><head><title>Hello World</title></head>")
sys.stdout.write("<body><h2>Hello CGI</h2></body></html>")
有什么帮助吗?#/usr/bin/python
#!/usr/bin/python
import sys
sys.stdout.write("Content-type: text/html \r\n\r\n")
sys.stdout.write("<!doctype html><html><head><title>Hello CGI</title></head>")
sys.stdout.write("<body><h2>Hello CGI</h2></body></html>")
导入系统
sys.stdout.write(“内容类型:text/html\r\n\r\n”)
sys.stdout.write(“Hello CGI”)
sys.stdout.write(“Hello CGI”)
并将premision添加到文件chmod+xquerys.py中,如果您尝试
localhost:8000/cgi-bin/
,则服务器正在寻找cgi-bin/index.html
来提供该文件,如果没有index.html
(并且您无权列出文件夹),则服务器发送错误403
。您是否添加了#/usr/bin/python
在hellocgi.py
中?Linux/Unix(和MacOS)需要这行代码才能使用Python运行脚本。如果您添加#/usr/bin/perl
它将尝试使用perl运行此脚本。Linux/Unix通过此行识别脚本,而不是通过文件扩展名。您是否尝试在命令行(控制台/终端)中运行hellocgi.py
,而不使用python
(不是python hellocgi.py
,而是hellocgi.py
或/hellocgi.py
)?谢谢您的回复。您的评论帮助我理解了一个简单的事实,即该文件没有执行权限,因此我使用命令sudo chmod+x hellocgi.py
授予它,然后我再次运行服务器并工作。非常感谢你的比赛!
#!/usr/bin/python
import sys
sys.stdout.write("Content-type: text/html \r\n\r\n")
sys.stdout.write("<!doctype html><html><head><title>Hello CGI</title></head>")
sys.stdout.write("<body><h2>Hello CGI</h2></body></html>")