23

When I run python -m SimpleHTTPServer 8000 or python -m CGIHTTPServer 8000 in my shell I am hosting the content of my current directory to the internet.

I would like to make the following cgi_script.py work correctly using the above command in the command line when I browse to 192.xxx.x.xx:8000/cgi_script.py

#!/usr/bin/env python
print "Content-Type: text/html"
print
print """\
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
"""

But this script is displayed literally and not only the "Hello World!" part. Btw I changed the file permissions to 755 for cgi_script.py as well as the folder I am hosting it from.

Bentley4
  • 10,678
  • 25
  • 83
  • 134
  • For people trying this out on mac or linux, be sure to have permissions that allow executing the file. In other words: `chmod +x cgi-bin/cgi_script.py` – greggles May 21 '15 at 23:17

6 Answers6

35

Try with python -m CGIHTTPServer 8000.

Note that you have to move the script to a cgi-bin or htbin directory in order to be runnable.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • This doesn't change anything unfortunately. (I don't understand why not). – Bentley4 May 01 '12 at 10:25
  • @Bentley4 - Please see the updated answer about the CGI directory. – rodrigo May 01 '12 at 10:38
  • I tried with the directory being named cgi_bin, htbin and even cgi-bin. None worked appropriately. – Bentley4 May 01 '12 at 11:12
  • 1
    @Bentley4 - My bad! It should be `cgi-bin` (use the Source!). Corrected. And it worked for me. But note that the url should be changed accordingly: `http://xxxx:8000/cgi-bin/cgi_script.py`. – rodrigo May 01 '12 at 11:17
  • Thank you! I see what I did wrong there. I have to run `python -m CGIHTTPServer 8000` from the directory one level under the `cgi-bin` directory. If you run it with ´cgi-bin´ as your current directory it won't work, strange. – Bentley4 May 01 '12 at 11:40
  • 2
    @Bentley4 - Not so strange, it is a classic old restriction to avoid running random programs as if they were CGI when they were not intended to be used that way. – rodrigo May 01 '12 at 12:35
  • Thanks! Your answer saved me! –  Aug 24 '17 at 20:07
6

SO doesn't allow me to comment so I'm adding this as a separate answer, addition to rodrigo's.

You can use another parameter cgi_directories which defaults to ['/cgi-bin', '/htbin']. More info here

spawnedc
  • 476
  • 5
  • 9
2

In Python3 the command line is simply

python3 -m http.server --cgi 8000
JP Ventura
  • 5,564
  • 6
  • 52
  • 69
AchimG
  • 21
  • 1
  • 1
    and yes: the script needs to reside in '/cgi-bin' – AchimG Feb 12 '15 at 09:37
  • 5
    your command fails with the error `Unknown option: -p` which makes sense because you are giving `http.server` as if it were a set of short options. It does not work like this. Instead it should be `python -m http.server --cgi 8000` – josch Jun 13 '15 at 02:44
0

When I ran into this issue I found that depending on which directory you are in when you run the python -m CGIHTTPServer 8000 command yields different results. When attempting to run the command while in the cgi-bin directory the browser continued to return the raw script code. once I cd'ed one level higher and ran the python -m CGIHTTPServer 8000 command again my script began executing.

Marquis Blount
  • 7,585
  • 8
  • 43
  • 67
0

@Bentley4 -ifyou are still not able to do, try importing cgi.

#!C:\Python34\python.exe -u import cgi print ("Content-type:text/html")

HTH

0

This work for me, run the python -m CGIHTTPServer 8000 command same menu level with cgi-bin,and move cgi_script.py into cgi-bin folder.In browser type http://localhost:8000/cgi-bin/cgi_script.py

Brian
  • 33
  • 6