如何使用 python http.server 运行 CGI“hello world”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30516414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:34:50  来源:igfitidea点击:

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

pythonhttppython-3.xcgi

提问by Yura

I am using Windows 7 and Python 3.4.3. I would like to run this simple helloworld.py file in my browser:

我使用的是 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>')

What I do is:

我要做的是:

1) Go to command line C:\Python(where python is installed)

1)进入命令行C:\Python(安装python的地方)

2) run: python -m http.server

2)运行: python -m http.server

3) Got to Firefox and type http://localhost:8000/hello.py

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

However, instead of "Hello World", the browser just prints the content of the hello.py file.

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

How can I fix it?

我该如何解决?

采纳答案by jfs

From the http.serverdocs:

http.server文档

CGIHTTPRequestHandlercan be enabled in the command line by passing the --cgioption:

CGIHTTPRequestHandler可以通过传递--cgi选项在命令行中启用:

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

Put your script into cgi_directories:

将您的脚本放入cgi_directories

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

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

Open in the browser:

在浏览器中打开:

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

where hello.py:

其中hello.py

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

I had to make it executable on POSIX: chmod +x cgi-bin/hello.py.

我不得不使其可执行的POSIX: chmod +x cgi-bin/hello.py

回答by Tom-Oliver Heidel

I did this some time ago for Python2.7

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

So in Python 3 you just need to change imports

所以在 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()

回答by Bruno Bronosky

I created a complete examplefor a friend. It is a complete demo you can run with 8 simple copy-paste ready lines of code. Enjoy.

我为朋友创建了一个完整的示例。这是一个完整的演示,您可以使用 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)