用于构建 TCP 服务器的好的 Python 网络库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/441849/
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
Good Python networking libraries for building a TCP server?
提问by ryeguy
I was just wondering what network libraries there are out there for Python for building a TCP/IP server. I know that Twisted might jump to mind but the documentation seems scarce, sloppy, and scattered to me.
我只是想知道有哪些用于 Python 构建 TCP/IP 服务器的网络库。我知道 Twisted 可能会跳到我的脑海中,但文档对我来说似乎稀缺、草率且零散。
Also, would using Twisted even have a benefit over rolling my own server with select.select()?
此外,使用 Twisted 甚至比使用 select.select() 滚动我自己的服务器有好处吗?
采纳答案by pboucher
I must agree that the documentation is a bit terse but the tutorial gets you up and running quickly.
我必须同意文档有点简洁,但本教程可以让您快速上手。
http://twistedmatrix.com/projects/core/documentation/howto/tutorial/index.html
http://twistedmatrix.com/projects/core/documentation/howto/tutorial/index.html
The event-based programming paradigm of Twisted and it's defereds might be a bit weird at the start (was for me) but it is worth the learning curve.
Twisted 的基于事件的编程范式及其延迟在开始时可能有点奇怪(对我来说),但值得学习曲线。
You'll get up and running doing much more complex stuff more quickly than if you were to write your own framework and it would also mean one less thing to bug hunt as Twisted is very much production proven.
与编写自己的框架相比,您将能够更快地启动并运行更复杂的东西,而且这也意味着寻找 bug 的事情少了一件,因为 Twisted 已经在生产环境中得到了充分的验证。
I don't really know of another framework that can offer as much as Twisted can, so my vote would definitely go for Twisted even if the docs aren't for the faint of heart.
我真的不知道还有什么框架可以提供与 Twisted 一样多的功能,所以我的投票肯定会投给 Twisted,即使这些文档不适合胆小的人。
I agree with Greg that SocketServer is a nice middle ground but depending on the target audience of your application and the design of it you might have some nice stuff to look forward to in Twisted (the PerspectiveBroker which is very useful comes to mind - http://twistedmatrix.com/projects/core/documentation/howto/pb-intro.html)
我同意 Greg 的观点,即 SocketServer 是一个不错的中间立场,但是根据您的应用程序的目标受众及其设计,您可能会在 Twisted 中期待一些不错的东西(想到非常有用的 PerspectiveBroker - http: //twistedmatrix.com/projects/core/documentation/howto/pb-intro.html)
回答by Greg Hewgill
The standard library includes SocketServer and related modules which might be sufficient for your needs. This is a good middle ground between a complex framework like Twisted, and rolling your own select() loop.
标准库包括 SocketServer 和相关模块,它们可能足以满足您的需求。这是在像 Twisted 这样的复杂框架和滚动你自己的 select() 循环之间的一个很好的中间地带。
回答by zenazn
If you're reluctant to use Twisted, you might want to check out SocketServer.ThreadingTCPServer. It's easy enough to use, and it's good enoughfor many purposes.
如果您不愿意使用 Twisted,您可能需要查看SocketServer.ThreadingTCPServer。它很容易使用,而且对于许多用途来说已经足够好了。
For the majority of situations, Twisted is probably going to be faster and more reliable, so I'd stomach the documentation if you can :)
在大多数情况下,Twisted 可能会更快、更可靠,所以如果可以的话,我会接受文档:)
回答by Claudiu
Just adding an answer to re-iterate other posters - it'll be worth it to use Twisted. There's no reason to write yet another TCP server that'll end up working not as well as one using twisted would. The only reason would be if writing your own is much faster, developer-wise, but if you just bite the bullet and learn twisted now, your future projects will benefit greatly. And, as others have said, you'll be able to do much more complex stuff if you use twisted from the start.
只需添加一个答案来重复其他海报 - 使用 Twisted 是值得的。没有理由再写一个 TCP 服务器,它的工作效果不如使用 Twisted 的服务器好。唯一的原因是,如果自己编写代码要快得多,这对开发人员来说是明智的,但是如果您现在硬着头皮学习,那么您未来的项目将受益匪浅。而且,正如其他人所说,如果您从一开始就使用扭曲,您将能够做更复杂的事情。
回答by rhettg
I've tried 3 approaches:
我尝试了 3 种方法:
- Write my own select() loop framework(pretty much dead, I don't necessarily recommend it.)
- Using SocketServer
- Twisted
I used the SocketServer for a internal web service with fairly low traffic. Is used for a fairly high traffic internal logging service. Both perform perfectly well and seem pretty reliable for production use. For anything that needs to be performant I think the Twisted stuff is much better, but it's a lot more work to get your head around the architecture.
我将 SocketServer 用于流量相当低的内部 Web 服务。用于相当高流量的内部日志服务。两者都表现得非常好,而且对于生产使用来说似乎非常可靠。对于任何需要高性能的东西,我认为 Twisted 的东西要好得多,但是要了解架构需要做更多的工作。