title: CGIHTTPServer and Python script
body: <p>I am currently writing a python program which prints HTML page using <code>sys.stdout.print()</code> the script name is hellocgi.py and it is inside a cgi-bin directory on the Desktop on my Mac OS X.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import sys
  sys.stdout.write("Content-type: text/html \r\n\r\n")
  sys.stdout.write("&lt;!doctype html&gt;&lt;html&gt;&lt;head&gt;&lt;title&gt;Hello World&lt;/title&gt;&lt;/head&gt;")
  sys.stdout.write("&lt;body&gt;&lt;h2&gt;Hello CGI&lt;/h2&gt;&lt;/body&gt;&lt;/html&gt;")
  </code></pre>
  
  <p>Then I am running the command <code>python -m CGIHTTPServer</code> inside the cgi-bin directory, and when I am going to the url <code>localhost:8000/hellocgi.py</code> what I get is the source code inside firefox.</p>
  
  <p>If I try to run <code>python -m CGIHTTPServer</code> outside the cgi-bin, I get an error on url <code>localhost:8000/cgi-bin/</code>:</p>
  
  <blockquote>
    <p>Error response</p>
    
    <p>Error code 403.</p>
    
    <p>Message: CGI script is not a plain file ('/cgi-bin/').</p>
    
    <p>Error code explanation: 403 = Request forbidden -- authorization will
    not help.</p>
  </blockquote>
  
  <p>On URL <code>localhost:8000/cgi-bin/hellocgi.py</code> firefox prompt me to download the source code.</p>
  
  <p>Python version: 2.7.5
  Mac OS X: 10.9</p>
  
  <p>python -m CGIHTTPServer log:</p>
  
  <pre><code>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 &lt;module&gt;
      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)
  </code></pre>
  
  <p>Any help?</p>