How to know when it's possible to bind a socket on an unprivileged port?
gnewsg at gmail.com
Thu Dec 11 19:53:03 CET 2008
On 11 Dic, 19:09, "Giampaolo Rodola'" <gne... at gmail.com> wrote:
> For a purpose of testing I need a function which could tell me whether
> it is possible to bind sockets on privileged ports or not.
> I wrote down this simple function. It seems reasonably working to me
> but I'd like to hear your opinion first.
> Thanks in advance.
> import socket, errno
> def bind_on_privileged_ports(port=21):
> """Return True if it is possible to bind sockets on privileged
> ports (< 1024)."""
> s = socket.socket()
> s.bind(("", port))
> except socket.error, err:
> if err == errno.EACCES:
> return False
> return True
Just to clarify: I don't really care *which* port to use for binding
the socket. I just need to try to bind a socket on a free random
privileged port and return True if that has been possible.
More information about the Python-list