[Twisted-Python] iConnector factory not what expected

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Fri Jun 15 09:15:43 EDT 2012

On 08:50 am, martin at webscio.net wrote:
>On 15/06/12 01:40, exarkun at twistedmatrix.com wrote:
>>On 14 Jun, 03:34 pm, martin at webscio.net wrote:
>>>Hi again,
>>>I've a bit of code that does the following:
>>>f = MyReconnectingClientFactory()
>>>iConn = reactor.connectSSL(url, port, f, ssl.ClientContextFactory())
>>>now I would expect that iConn.factory would be my f, but as it turns
>>>it's an instance of some TLSMemoryBIOFactory.. Why is that so?
>>>After some digging, I was able to find f under
>>>iConn.transport.protocol.wrappedProtocol.factory.. I'm happy to use
>>>that, but it just sounds a bit weird to me.. or is this normal?
>>There is no "factory" attribute on the `IConnector` interface at all:
>>Neither is there a "transport" attribute, nor do any of the transport
>>interfaces have a "protocol" attribute.
>>All this means that Twisted isn't promising you anything about the
>>meaning or existence of any of these attributes.
>>So, why don't you just use `f` instead?
>>Twisted-Python mailing list
>>Twisted-Python at twistedmatrix.com
>I can't use f because I'm saving the IConnector object for later, so
>that I can disconnect it when needed. I'm not sure if saving the f
>object would have any effect, since doing anything to that after the
>connection was created will surely have no effect, or?

This doesn't make sense.  `f` is not an object and neither is 
`connector.factory` nor 
`connector.transport.protocol.wrappedProtocol.factory`.  They're 
expressions that, at least in this case, and in the context you're using 
them, all evaluate to the same object: the MyReconnectingClientFactory 
instance created at the beginning of your example code.

It doesn't matter which of them you call stopTrying on, since there 
isn't actually any "them", there's just the single factory object.

Put another way, saving `f` will let you do exactly what you want, in 
part because it's no different from what the `IConnector` implementation 
happens to be doing.

>In general, I can do iConn.disconnect(), but this being a
>ReconnectingClientFactory, it attempts to reconnect straight away..
>That's why I was looking to get the factory object somehow, so that I
>could call stopTrying() on it first. If there's a better approach to
>this, please let me know :)
>Twisted-Python mailing list
>Twisted-Python at twistedmatrix.com

More information about the Twisted-Python mailing list