[Twisted-Python] How to get an unused port

Gabriel Rossetti mailing_lists at evotex.ch
Wed Feb 27 05:36:51 EST 2008


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

I tried running reactor.run() but I get an exception :

exceptions.AttributeError: Factory instance has no attribute 
'clientConnectionFailed'

I though the ClientFactory had that, I must be wrong, I could of course 
subclass it and add it but it's a bit "complicated" for just getting a 
free port, so I may have done something wrong...

Gabriel

> Hope that helps,
>
>
> Matt   
>
> Matthew Glubb
> Technical Partner
>
> email: matthew.glubb at madebykite.com
> phone: 44 (0) 7715 754017
> skype: mglubb
>
> Kite
> http://madebykite.com
>
> -- 
> GPG: 96FF DE0E 0B7B 37F0 7F8D C54C E285 3D8F 5625 9244
>
> On 27 Feb 2008, at 09:06, Gabriel Rossetti wrote:
>
>> Hello,
>>
>> I need to be able to get an unused port for a server, it will then 
>> communicate it to a third party.
>> I don't have any restrictions other than it shouldn't be a known used 
>> port (one used by other apps (above 1023)), but for that I'll 
>> probably have to create a list and verify manually. Does anyone know 
>> how to do this (without iterating all ports and trying), it must be 
>> possible since a client gets the next unused port from the OS, I'd 
>> like to do this with Twisted if possible.
>>
>> Thanks,
>> Gabriel
>>
>> _______________________________________________
>> Twisted-Python mailing list
>> Twisted-Python at twistedmatrix.com
>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>





More information about the Twisted-Python mailing list