簡體   English   中英

CGIHTTPServer和Python腳本

[英]CGIHTTPServer and Python script

我目前正在編寫一個使用sys.stdout.print()打印HTML頁面的python程序,腳本名稱為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>")

然后,我在cgi-bin目錄中運行命令python -m CGIHTTPServer ,當我轉到url localhost:8000/hellocgi.py ,得到的是firefox中的源代碼。

如果我嘗試在cgi-bin之外運行python -m CGIHTTPServer ,則會在url localhost:8000/cgi-bin/上收到錯誤消息:

錯誤回應

錯誤代碼403。

消息:CGI腳本不是純文件('/ cgi-bin /')。

錯誤代碼說明:403 =禁止請求-授權將無濟於事。

在URL localhost:8000/cgi-bin/hellocgi.py firefox提示我下載源代碼。

python版本:2.7.5 Mac OS X:10.9

python -m CGIHTTPServer日志:

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)

有什么幫助嗎?

#!/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>")

並將premision添加到文件chmod + x querys.py中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM