如何 运行 CGI "hello world" 与 python http.server

How to run CGI "hello world" with python http.server

我正在使用 Windows 7 和 Python 3.4.3。我想在我的浏览器中 运行 这个简单的 helloworld.py 文件:

print('Content-Type: text/html')
print( '<html>')
print( '<head></head>')
print( '<body>')
print( '<h2>Hello World</h2>')
print( '</body></html>')

我做的是:

1) 转到命令行 C:\Python(其中安装了 python)

2) 运行: python -m http.server

3) 进入 Firefox 并输入 http://localhost:8000/hello.py

但是,浏览器只是打印 hello.py 文件的内容,而不是 "Hello World"。

我该如何解决?

我前段时间为 Python2.7

做了这个
from BaseHTTPServer import BaseHTTPRequestHandler

class GetHandler(BaseHTTPRequestHandler):

    def do_HEAD(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()

    def do_GET(self):
        x = self.wfile.write
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        # <--- HTML starts here --->
        x("<html>")
        # <--- HEAD starts here --->
        x("<head>")
        x("<title>Title goes here!</title>")
        x("</head>")
        # <--- HEAD ends here --->
        # <--- BODY starts here --->
        x("<body>")
        x("<p>This is a test.</p>")
        x("</body>")
        # <--- BODY ends here --->
        x("</html>")
        # <--- HTML ends here --->

if __name__ == '__main__':
    from BaseHTTPServer import HTTPServer
    server = HTTPServer(('localhost', 777), GetHandler)
    print 'Starting server, use <Ctrl + F2> to stop'
    server.serve_forever()

所以在 Python 3 中你只需要改变导入

from http.server import BaseHTTPRequestHandler

class GetHandler(BaseHTTPRequestHandler):

    def do_HEAD(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()

    def do_GET(self):
        x = self.wfile.write
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        # <--- HTML starts here --->
        x("<html>")
        # <--- HEAD starts here --->
        x("<head>")
        x("<title>Title goes here!</title>")
        x("</head>")
        # <--- HEAD ends here --->
        # <--- BODY starts here --->
        x("<body>")
        x("<p>This is a test.</p>")
        x("</body>")
        # <--- BODY ends here --->
        x("</html>")
        # <--- HTML ends here --->

if __name__ == '__main__':
    from http.server import HTTPServer
    server = HTTPServer(('localhost', 777), GetHandler)
    print('Starting server, use <Ctrl + F2> to stop')
    server.serve_forever()

来自 the http.server docs:

CGIHTTPRequestHandler can be enabled in the command line by passing the --cgi option:

$ python3 -m http.server --bind localhost --cgi 8000

将脚本放入 cgi_directories:

This defaults to ['/cgi-bin', '/htbin'] and describes directories to treat as containing CGI scripts.

在浏览器中打开:

$ python -mwebbrowser http://localhost:8000/cgi-bin/hello.py

其中 hello.py:

#!/usr/bin/env python3
print("Content-Type: text/html\n")
print("<!doctype html><title>Hello</title><h2>hello world</h2>")

我必须让它在 POSIX 上可执行:chmod +x cgi-bin/hello.py

我为朋友创建了一个 complete example。这是一个完整的演示,您可以 运行 使用 8 行简单的 copy-paste 就绪代码。享受吧。

echo -e "\n\n    Usage: after running this script, visit http://localhost:8000/cgi-bin/hello    \n\n"
mkdir /tmp/cgi-bin/
cat > /tmp/cgi-bin/hello <<EOF
#!/bin/bash
echo -e "Content-Type: text/plain\n\n"; date; echo; env
EOF
chmod +x /tmp/cgi-bin/hello
(cd /tmp; python3 -m http.server --cgi 8000)