Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm not at all knowledgeable about this, so please bear with me. I'm trying to set up python's CGIHTTPServer on OSX to be able to serve cgi-scripts locally, but I seem to be unable to do this.

I've got a simple test script:

#!/usr/bin/env python

import cgi

cgi.test()

(with permissions -rwxr-xr-x@ that's located in ~/WWW (with permissions `drwxr-xr-x) that runs just fine from the shell and I have this script to serve them using CGIHTTPServer:

import CGIHTTPServer
import BaseHTTPServer

class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
    cgi_directories = ["~/WWW"]

PORT = 8000

httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
print "serving at port", PORT

but when I run it, going to localhost:8000 just serves the content of the script, not the result (i.e. it gives back the code, not the output).

What am I doing wrong?

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

The problem is that the server isn't recognizing ~/WWW as the CGI directory and you're just browsing a file. Note the server's path starts at whatever your current working directory is.

I have a server.py as you do above, except with an absolute path and running the server in a loop.

Full code:

$ cd ~
$ mkdir WWW
$ cat > server.py
#!/usr/bin/env python
import CGIHTTPServer
import BaseHTTPServer

class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
    cgi_directories = ["/WWW"]

PORT = 8000
httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
^D
$ cat > WWW/test.py
#!/usr/bin/env python
import cgi
cgi.test()
^D
$ chmod +x server.py WWW/test.py
$ ./server.py &
$ # browse to http://127.0.0.1:8000/WWW/test.py
share|improve this answer
    
yes! that did it! thanks a bunch –  thepandaatemyface Aug 7 '10 at 22:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.