[Twisted-Python] Re: How to get an unused port

Gabriel Rossetti mailing_lists at evotex.ch
Wed Feb 27 05:56:50 EST 2008


Martin Geisler wrote:
> Gabriel Rossetti <mailing_lists at evotex.ch> writes:
>
>   
>> Matthew Glubb wrote:
>>     
>>> Hi Gabriel,
>>>
>>> If memory serves me correctly, when calling reactor.ConnectTCP,
>>> specify zero for your port, you'll automatically get an unused port
>>> from the dynamic range.
>>>
>>>       
>> Thank you, unfortunately I tried it and got 0 back :
>>
>>     
>>>>> reactor.connectTCP("localhost", 0, ClientFactory()).port
>>>>>           
>> 0
>>     
>>>>> reactor.connectTCP("localhost", 0,
>>>>>           
>> ClientFactory()).getDestination().port
>> 0
>>     
>
> You will never want to connect to port 0 -- there is no such port as
> far as I know. Instead you can listen on it. The OS will then allocate
> a free port for you to listen on.
>
> So I think you have turned things upside down here :-)
>
>   
Yes, I realized that just after sending the email :-)

like you said :

 >>> reactor.listenTCP(0, Factory()).getHost().port
59768

or

 >>> reactor.listenTCP(0, Factory())._realPortNumber
47315

work :-)

thanks again to everyone who helpd,
Gabriel







More information about the Twisted-Python mailing list