how to get any available port

Grant Edwards grante at visi.com
Tue Oct 4 17:40:03 EDT 2005


On 2005-10-04, Paul Rubin <> wrote:
> Mohammed Smadi <smadim2 at grads.ece.mcmaster.ca> writes:
>> #transmission socket
>> s = socket.socket(socket.AF_INET,  socket.SOCK_STREAM)
>> s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
>> s.bind(("",hp_port)) # do some error checking
>> ...
>> any suggestions for alternative implementation?
>
> In this particular instance, don't bind the transmission socket.  I'm
> also not sure why you set SO_REUSEADDR.  I've done some socket
> programming but am no expert.

You set SO_REUSEADDR when you need to bind to a specific port,
and you want to be able to re-use that port address for a new
connection without waiting for the time specified in the TCP
RFC after the old connection was closed.

In the OP's case, there is no need to bind to a specific port,
so just getting rid of the bind() and setsockopt() calls is the
obvious answer.

-- 
Grant Edwards                   grante             Yow!  If I had a Q-TIP, I
                                  at               could prevent th' collapse
                               visi.com            of NEGOTIATIONS!!



More information about the Python-list mailing list