首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGIHTTPServer和Python

CGIHTTPServer和Python
EN

Stack Overflow用户
提问于 2014-06-24 15:40:20
回答 1查看 3.2K关注 0票数 0

我目前正在编写一个python程序,它使用sys.stdout.print()打印HTML页面,脚本名为hellocgi.py,它位于Mac上桌面上的cgi-bin目录中。

以下是代码:

代码语言:javascript
复制
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>")

然后,我在cgi-bin目录中运行命令python -m CGIHTTPServer,当我要访问url localhost:8000/hellocgi.py时,我得到的是火狐内部的源代码。

如果我试图在cgi-bin之外运行python -m CGIHTTPServer,则在url localhost:8000/cgi-bin/上会出现错误。

误差响应 错误代码403 消息: CGI脚本不是普通文件(‘/cgi/’)。 错误代码解释: 403 =请求被禁止-授权不会有帮助。

在网址localhost:8000/cgi-bin/hellocgi.py火狐提示我下载源代码。

Python版本:2.7.5MacOSX: 10.9

python -m CGIHTTPServer日志:

代码语言:javascript
复制
Serving HTTP on 0.0.0.0 port 8000 ...
127.0.0.1 - - [24/Jun/2014 18:34:49] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [24/Jun/2014 18:34:51] code 403, message CGI script is not a plain file ('/cgi-bin/')
127.0.0.1 - - [24/Jun/2014 18:34:51] "GET /cgi-bin/ HTTP/1.1" 403 -
127.0.0.1 - - [24/Jun/2014 18:36:01] "GET /cgi-bin/hellocgi.py HTTP/1.1" 200 -
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/CGIHTTPServer.py", line 253, in run_cgi
    os.execve(scriptfile, args, env)
OSError: [Errno 13] Permission denied
127.0.0.1 - - [24/Jun/2014 18:36:01] CGI script exit status 0x7f00
^CTraceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/CGIHTTPServer.py", line 378, in <module>
    test()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/CGIHTTPServer.py", line 374, in test
    SimpleHTTPServer.test(HandlerClass, ServerClass)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SimpleHTTPServer.py", line 216, in test
    BaseHTTPServer.test(HandlerClass, ServerClass)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 599, in test
    httpd.serve_forever()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 236, in serve_forever
    poll_interval)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 155, in _eintr_retry
    return func(*args)

有什么帮助吗?

EN

回答 1

Stack Overflow用户

发布于 2015-02-11 19:51:53

代码语言:javascript
复制
#!/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>")

并添加到文件chmod +x querys.py中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24390888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档