views:

909

answers:

5

Hi all,

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.

Also, would using Twisted even have a benefit over rolling my own server with select.select()?

+3  A: 

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.

Greg Hewgill
It seems that you need to use threads to support multiple client handling. Wouldn't the overhead from creating/destroying all those threads make it worthless? Wouldn't select() be better then?
ryeguy
@ryeguy: One need not use threads at all with SocketServer, in fact the documentation <http://docs.python.org/library/socketserver.html> mentions this in the last paragraph of the "Server Creation Notes" section.
Greg Hewgill
+1: It's already in the library, it already works.
S.Lott
+9  A: 

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

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.

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.

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.

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)

pboucher
+1  A: 

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 enough for many purposes.

For the majority of situations, Twisted is probably going to be faster and more reliable, so I'd stomach the documentation if you can :)

zenazn
+1  A: 

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.

Claudiu
+1  A: 

I've tried 3 approaches:

  • 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.

rhettg