Python 中的嵌入式 Web 服务器?

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

Embedded Web Server in Python?

pythonsimplehttpserverembeddedwebserver

提问by Ankur Gupta

Can you recommend a minimalistic python webserver that I can embedded in my Desktop Application.

你能推荐一个我可以嵌入到我的桌面应用程序中的简约 python 网络服务器吗?

回答by Tim Lesher

How minimalistic and for what purpose?

多么简约,目的是什么?

SimpleHTTPServercomes free as part of the standard Python libraries.

SimpleHTTPServer作为标准 Python 库的一部分免费提供。

If you need more features, look into CherryPyor (at the top end) Twisted.

如果您需要更多功能,请查看CherryPy或(在顶端)Twisted

回答by Matthew Trevor

I'm becoming a big fan of the newly released circuitslibrary. It's a component/event framework that comes with a very nice set of packages for creating web servers & apps. Here's the simple web example from the site:

我正在成为新发布的电路库的忠实粉丝。它是一个组件/事件框架,带有一组非常好的用于创建 Web 服务器和应用程序的包。这是该站点的简单 Web 示例:

from circuits.lib.web import Server, Controller

class HelloWorld(Controller):
   def index(self):
      return "Hello World!"

server = Server(8000)
server += HelloWorld()
server.run()

Its WSGI support is no more complicated than that, either. Good stuff.

它的 WSGI 支持也没有比这更复杂。好东西。

回答by Jason Baker

If you're doing a lot of concurrent stuff, you might consider Kamaelia's HTTPServer.

如果你正在做很多并发的事情,你可能会考虑KamaeliaHTTPServer

回答by interstar

I've found web.py pretty easy to use : http://webpy.org/

我发现 web.py 非常易于使用:http: //webpy.org/

回答by ianb

If you want to use something from the standard library I would strongly recommend notusing SimpleHTTPServer, but instead using wsgiref.simple_server. SimpleHTTPServer is awkward and a rather nonsensical way to implement a web application, and while raw WSGI isn't terribly easy (but certainly possible), you have the option to use any WSGI-based framework on top of it. Also if you use wsgiref you will have the option to change to a server like CherryPy later (note that the server in CherryPy can be used separately from the rest of the framework, and you only need one filefor just the server). For a "real" web application CherryPy has several advantages over wsgiref, but for a locally hosted application it's unlikely any of them will matter.

如果你想使用标准库中的东西,我强烈建议不要使用 SimpleHTTPServer,而是使用wsgiref.simple_server. SimpleHTTPServer 是一种笨拙且相当荒谬的实现 Web 应用程序的方式,虽然原始 WSGI 不是非常容易(但肯定可能),但您可以选择在其上使用任何基于 WSGI 的框架。此外,如果您使用 wsgiref,您将可以选择稍后更改为像 CherryPy 这样的服务器(请注意,CherryPy 中的服务器可以与框架的其余部分分开使用,并且您只需要一个文件用于服务器)。对于“真正的”Web 应用程序,CherryPy 比 wsgiref 有几个优点,但对于本地托管的应用程序,它们中的任何一个都不重要。

If you are making a desktop application you will need to launch a separate thread for either wsgiref or CherryPy. If that's fine, then a WSGI-based server would probably be easiest. If you don't want to launch a separate thread for the server then you most likely need to use Twisted.

如果您正在制作桌面应用程序,您将需要为 wsgiref 或 CherryPy 启动一个单独的线程。如果没问题,那么基于 WSGI 的服务器可能是最简单的。如果您不想为服务器启动一个单独的线程,那么您很可能需要使用 Twisted。

回答by S.Lott

See the WSGI referenceimplementation.

请参阅WSGI 参考实现。