simple http server

Kevin Altis altis at
Wed Mar 17 19:31:41 CET 2004

"Gelo Ilzi" <geloilzi at> wrote in message
news:c39rul$dri$1 at
> I'm trying to implement a very simple http server with cgi functionality.
> The code is simple:
>    import CGIHTTPServer, BaseHTTPServer
>    httpd = BaseHTTPServer.HTTPServer(('',8000),
> CGIHTTPServer.CGIHTTPRequestHandler)
>    httpd.serve_forever()
> I've created a subdir 'cgi-bin' which contains a python script. Now when I
> enter this directory, i.e., go to the url http://localhost:8000/cgi-bin/
> browser shows:
>    Error response
>    Error code 403.
>    Message: CGI script is not a plain file ('/cgi-bin/').
>    Error code explanation: 403 = Request forbidden -- authorization will
> help.
> Plain html pages are ok.
> It seems I misunderstand something very basical. Could somebody help me
> please.
> Thank you in advance.

That is correct behavior, you can't browse the cgi-bin directory. Put a CGI
script in that directory like the one below to dump the environment
variables and then go directly to that URL. Depending on your platform and
location of Python, you might have to change the first line, but this will
verify your CGIs are working.


print "Content-type: text/html\r\n\r\n",
print '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'
print "<html><body>"
print "<pre>"
import os, sys
from cgi import escape
print "<strong>Python %s</strong>" % sys.version
keys = os.environ.keys()
for k in keys:
    print "%s\t%s" % (escape(k), escape(os.environ[k]))
print "</pre>"
print "</body></html>"

More information about the Python-list mailing list