how to get any available port

Fredrik Lundh fredrik at pythonware.com
Tue Oct 4 17:35:45 EDT 2005


Mohammed Smadi wrote:

> what else would you do?  I am using examples from the web and they all
> bind to a port at the localhost before connecting to the remote host.

pointers, please.

> my code is like this
>
> #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

that's a typical server setup.

> data="HI"
> print data
> s.connect(('192.168.2.13',port))
> s.send(data)

and this is typical client setup.

are you sure you're not cutting and pasting code from different examples?

> any suggestions for alternative implementation?

if you're writing a client, get rid of the setsockopt and bind stuff.

</F> 






More information about the Python-list mailing list