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

时间:2015-05-28 20:23:42

标签: python http python-3.x cgi

我使用的是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文件的内容而不是&#34; Hello World&#34;。

我该如何解决?

3 个答案:

答案 0 :(得分:14)

来自the http.server docs

  可以通过传递在命令行中启用

CGIHTTPRequestHandler   --cgi选项:

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

将您的脚本放入cgi_directories

  

默认为['/cgi-bin', '/htbin'],并描述要视为包含CGI脚本的目录。

在浏览器中打开:

$ 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上可执行。

答案 1 :(得分:2)

我前段时间为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()

我现在还不知道python 3中的print函数是

print("")

print ""

但我想这是()

编辑:它是print()

答案 2 :(得分:1)

我为朋友创建了complete example。这是一个完整的演示,您可以使用8个简单的复制粘贴准备好的代码行运行。享受。

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)