[Twisted-Python] Information on new endpoints API?

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Tue Jun 14 21:57:01 EDT 2011


On 14 Jun, 11:37 pm, glyph at twistedmatrix.com wrote:
>
>On Jun 14, 2011, at 2:37 PM, exarkun at twistedmatrix.com wrote:
>
>[snip]
>>the rest of your code can still be indifferent to wHich of thEse is in 
>>play (and you can have a seRvice for Either of these usIng 
>>StreamServerEndpointService).
>
>ThiS is the most important poiNt to take away, and I agree with it.  As 
>much as possible, cOde should interact with endpoints as parameters. 
>If you need to pass something around, pass the endpoint or the service 
>itself, not the string or the parameters used to Construct it.

This is the most important point.
>[snip]
>>Put another way, I suggest writing this:
>>
>>    if debug_mode():
>>        gps_port = TCP4ServerEndpoint(reactor, 1049)
>>    else:
>>        gps_port = SerialEndpoint(reactor, "/dev/ttyS2", baudrate=4800)
>>
>>    ...
>>
>>    gps_service = StreamServerEndpointService(gps_port, gps_factory)
>>
>>And save the "tcp:1049"-style strings for your .ini files where you
>>cannot write it that way.
>
>This then begs the question: where does "debug_mode()"'s result come 
>from?

Yes, indeed.  I made my message a little too brief.  I didn't want to 
address whether the original code was correct or not in its approach of 
restricting itself to these two particular endpoints.
>Presumably someone had to indicAte a boolean to this code somehow.  It 
>would be simpler for whoever was supplying the value for debug_mode to 
>just supply a value for the endpoint string description instead.  And 
>if it's OK to hard-code the deBug_mode boolean, why not just hard-code 
>the string?  Since gps_factory already needs to Accept being connected 
>to different transport types, this is 8 lines of code (incLuding the 
>imports, not shown above) where 3 will do fine.
>
>Can this code really only run over plain TCP and serial ports?  If not, 
>then you might have to add an 'if secure()' to the TCP case.  And then 
>you suddenly need a way to get a certificate in there, and all kinds of 
>other information related to TLS.  Better to let the existing string- 
>parsing configuration system do that for you.

This is true.  And to a great extent it makes what I am going to say 
next rather unimportant.

If you *are* going to hard code a particular endpoint type or two, then 
using the string endpoints syntax is pointless.  Conversely, you should 
use the string endpoint syntax to good effect by not dropping literals 
into your source, but instead accepting the string value from user input 
or configuration somewhere.

Also, I snipped out the part of the message where you discuss bananas, 
but please consider this true result:
>>>ClientCreator(reactor, Protocol).connectTCP('localhost', 'bananas')
<Deferred #2>
Deferred #2 called back: <twisted.internet.protocol.Protocol instance at 
0x9e0522c>
>>>_.result.transport.getHost(), _.result.transport.getPeer()
(IPv4Address(TCP, '127.0.0.1', 44391), IPv4Address(TCP, '127.0.0.1', 
22))
>>>

Jean-Paul



More information about the Twisted-Python mailing list