[Twisted-Python] Information on new endpoints API?

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Sun Jun 12 09:22:35 EDT 2011


On 12:48 pm, orestis at orestis.gr wrote:
>Hello,
>
>I'm curious about the endpoints API that appeared. I've read the 
>documentation, and I think I understand what it does, but I'm curious 
>to see what parts of twisted it replaces. A lot of the documentation 
>on-site is not updated (eg the finger tutorial) and it's not very clear 
>whether one should use reactor.listenTCP, TCPServer or the new API.

It's still often a good idea to use reactor.listenTCP.  If you know you 
want to run a TCP server, it's less typing than using an endpoint. 
Compare:

    reactor.listenTCP(1234, someFactory)
    TCP4ServerEndpoint(reactor, 1234).listen(someFactory)

Likewise, compare the service forms:

    TCPServer(1234, someFactory)
    StreamServerEndpointService(TCP4ServerEndpoint(reactor, 1234), 
someFactory)

However, if you want to write code that *isn't* hard-coded to TCP, then 
endpoints become much more attractive.  I won't even try to give an 
example of how you'd do that with the reactor listen* methods or the 
other services in twisted.application.internet.  But with endpoints, 
it's simple:

    someEndpoint.listen(someFactory)

or

    StreamServerEndpointService(someEndpoint, someFactory)

If you want the listen address to be configurable via some configuration 
file, or perhaps even in a GUI, 
`twisted.internet.endpoints.serverFromString` is one way you might get 
this endpoint (in particular, I would emphasize that serverFromString is 
really only meant to construct an endpoint if you *have* to start with a 
string - as you do when you are reading a configuration file).

Or you might have some other part of your application that directly 
constructs endpoints itself, rather than parsing strings, and passes 
them in to the part of code that is responsible for actually doing the 
listening
This is really why we introduced endpoints - to make it possible for 
applications to listen on (or connect to) an address that is specified 
elsewhere.  Previously, the "easy thing to do" was to only support 
TCP/IPv4.  With endpoints, the "easy thing to do" is to work with any 
address family/transport type that there is now (or will be in the 
future!) an endpoint for.
>
>I will be giving a training on Twisted in a few days during the 
>EuroPython conference (actually, more of an assisted tutorial than a 
>training) and I'm not sure how I should present this. Any information 
>would be helpful.
>
>Orestis

Hope this helps!  Good luck with the session. :)

Jean-Paul



More information about the Twisted-Python mailing list