windows 如何设置 Python CGI 服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8156898/
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-09-15 18:29:02  来源:igfitidea点击:

How do I set up a Python CGI server?

pythonhtmlwindowshttpcgi

提问by dln385

I'm running Python 3.2 on Windows. I want to run a simple CGI server on my machine for testing purposes. Here's what I've done so far:

我在 Windows 上运行 Python 3.2。我想在我的机器上运行一个简单的 CGI 服务器以进行测试。这是我到目前为止所做的:

I created a python program with the following code:

我用以下代码创建了一个python程序:

import http.server
import socketserver
PORT = 8000
Handler = http.server.CGIHTTPRequestHandler
httpd = socketserver.TCPServer(("", PORT), Handler)
httpd.serve_forever()

In the same folder, I created "index.html", a simple HTML file. I then ran the program and went to http://localhost:8000/in my web browser, and the page displayed successfully. Next I made a file called "hello.py" in the same directory, with the following code:

在同一个文件夹中,我创建了“index.html”,一个简单的 HTML 文件。然后我运行程序并在浏览器中访问http://localhost:8000/,页面显示成功。接下来我在同一个目录下创建了一个名为“hello.py”的文件,代码如下:

import cgi
import cgitb
cgitb.enable()
print("Content-Type: text/html;charset=utf-8")
print()
print("""<html><body><p>Hello World!</p></body></html>""")

Now if I go to http://localhost:8000/hello.py, my web browser displays the full code above instead of just "Hello World!". How do I make python execute the CGI code before serving it?

现在,如果我转到http://localhost:8000/hello.py,我的 Web 浏览器会显示上面的完整代码,而不仅仅是“Hello World!”。如何让 python 在提供 CGI 代码之前执行它?

采纳答案by Thomas K

Take a look at the docs for CGIHTTPRequestHandler, which describe how it works out which files are CGI scripts.

看看CGIHTTPRequestHandler文档,它描述了它如何确定哪些文件是 CGI 脚本。

回答by SingleNegationElimination

Though not officialy deprecated, the cgimodule is a bit clunky to use; most folks these days are using something else (anything else!)

虽然没有正式弃用,但该cgi模块使用起来有点笨拙;现在大多数人都在使用其他东西(其他任何东西!)

You can, for instance, use the wsgi interface to write your scripts in a way that can be easily and efficiently served in many http servers. To get you started, you can even use the builtin wsgirefhandler.

例如,您可以使用 wsgi 接口以一种可以在许多 http 服务器中轻松有效地提供服务的方式编写脚本。为了让您开始,您甚至可以使用内置wsgiref处理程序。

def application(environ, start_response):
    start_response([('content-type', 'text/html;charset=utf-8')])
    return ['<html><body><p>Hello World!</p></body></html>'.encode('utf-8')]

And to serve it (possibly in the same file):

并提供它(可能在同一个文件中):

import wsgiref.simple_server
server = wsgiref.simple_server.make_server('', 8000, application)
server.serve_forever()

回答by gelonida

simplest way to start a cgi server for development is following:

启动 cgi 服务器进行开发的最简单方法如下:

  • create a base directory with all your html and other files
  • create a subdirectory named cgi-binwith all your cgi files
  • cd to the base directory
  • run python -m http.server --cgi -b 127.0.0.1 8000
  • 创建一个包含所有 html 和其他文件的基本目录
  • 创建一个以cgi-bin所有 cgi 文件命名的子目录
  • cd 到基本目录
  • python -m http.server --cgi -b 127.0.0.1 8000

Now you can connect to http://localhost:8000and tst your html code with cgi scripts

现在您可以连接到http://localhost:8000并使用 cgi 脚本测试您的 html 代码