开发者

CGIHTTPServer: Error code 403

i got a:

Error response Error code 403. Message: CGI script is not executable ('/cgi-bin/main.pyc'). Error code explanation: 403 = Request forbidden -- authorization wi开发者_如何转开发ll not help.

while tying to run a compiled python script (.pyc) on a python CGIHTTPServer. normal python scripts (.py) are working fine. cgi server looks like this:

from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler

class CGIServer(HTTPServer):
    def __init__(self, (hostname,port), handler):
        HTTPServer.__init__(self, (hostname, port), handler)
srvaddr = ("", 8000)
cgisrv = CGIServer(srvaddr,CGIHTTPRequestHandler)
cgisrv.serve_forever()

is there any possibility to get this working on windows? .pyc files are linked like .py files under windows. even google can't tell me more.

Thanks


.pyc files are compiled Python scripts, meant to be run on their own. I am rather sure that CGIHTTPServer cannot run compiled Python files, only scripts with the .py extension.

Is there any reason you can't use a .py instead?


You need to write a tiny wrapper (in normal python) to load the other code as a module.

For more details, please consider the python tutorial [1] and its comments:

  • A program doesn't run any faster when it is read from a ".pyc" or ".pyo" file than when it is read from a ".py" file; the only thing that's faster about ".pyc" or ".pyo" files is the speed with which they are loaded.

  • When a script is run by giving its name on the command line, the bytecode for the script is never written to a ".pyc" or ".pyo" file. Thus, the startup time of a script may be reduced by moving most of its code to a module and having a small bootstrap script that imports that module.

FYI - I just tested CGIHTTPServer with a script and the compiled version of that same script. I got the error you noted, and when I tried to run the two versions on the command line, running the .py version printed the expected output, and running the .pyc version caused a bunch of garbage to be printed on the terminal (I didn't bother to investigate the exact source). In summary - double check that you can run a program on the command line as a basic test.

[1] http://docs.python.org/release/1.5.1p1/tut/node43.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜