0

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?

1 Answers1

1

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
medina
  • 1,970
  • 10
  • 7