Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CGIHTTPServer和Python脚本_Python_Macos_Cgi - Fatal编程技术网

CGIHTTPServer和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<

我目前正在编写一个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</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>")