How to host python cgi script with `python -m SimpleHTTPServer 8000` or `python -m CGIHTTPServer 8000`?

点点圈 提交于 2019-11-27 02:11:16

问题


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.


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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

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

HTH




回答5:


in Python3 the command line is simply

python -http.server --cgi 8000



回答6:


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



来源:https://stackoverflow.com/questions/10396330/how-to-host-python-cgi-script-with-python-m-simplehttpserver-8000-or-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!