Re: choosing random dynamic port number

Discussion in 'Python' started by Fredrik Lundh, Jan 3, 2008.

  1. Emin.shopper Martinian.shopper wrote:

    > Is there a good way to choose/assign random dynamic port numbers in python?
    >
    > I had in mind something like the following, but if multiple programs are
    > generating random port numbers, is there a way to check if a given port
    > number is already taken?
    >
    > def GenerateDynamicPortNumber():
    > "Generate a random dynamic port number and return it."
    > # port numbers between 49152 to 65535 are dynamic port numbers
    > return 49152 + random.randrange(15000)


    def GenerateDynamicPortNumber():
    return 0

    (to get the actual number, use getsockname() on the socket after you've
    called "bind" on it)

    </F>
    Fredrik Lundh, Jan 3, 2008
    #1
    1. Advertising

  2. On 3 Gen, 23:21, Fredrik Lundh <> wrote:
    > Emin.shopper Martinian.shopper wrote:
    > > Is there a good way to choose/assign random dynamic port numbers in python?

    >
    > > I had in mind something like the following, but if multiple programs are
    > > generating random port numbers, is there a way to check if a given port
    > > number is already taken?

    >
    > >     def GenerateDynamicPortNumber():
    > >         "Generate a random dynamic port number and return it."
    > >         # port numbers between 49152 to 65535 are dynamic port numbers
    > >         return 49152 + random.randrange(15000)

    >
    >     def GenerateDynamicPortNumber():
    >         return 0
    >
    > (to get the actual number, use getsockname() on the socket after you've
    > called "bind" on it)
    >
    > </F>


    By using 0 as port number value you let kernel choose a free
    unprivileged random port:

    >>> import socket
    >>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    >>> s.bind(('', 0))
    >>> s.getsockname()

    ('0.0.0.0', 3070)
    Giampaolo Rodola', Jan 4, 2008
    #2
    1. Advertising

  3. On 2008-01-04, Giampaolo Rodola' <> wrote:

    >>     def GenerateDynamicPortNumber():
    >>         return 0
    >>
    >> (to get the actual number, use getsockname() on the socket after you've
    >> called "bind" on it)
    >>
    >> </F>

    >
    > By using 0 as port number value you let kernel choose a free
    > unprivileged random port:


    The port number chosen isn't random on many OSes. If the OP
    really wants a random port number, he'll have to generate it
    himself.

    --
    Grant Edwards grante Yow! Look! A ladder!
    at Maybe it leads to heaven,
    visi.com or a sandwich!
    Grant Edwards, Jan 4, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Digital Puer
    Replies:
    5
    Views:
    11,835
    marcus
    Nov 29, 2004
  2. xeys_00
    Replies:
    12
    Views:
    852
    Jonathan Arnold
    Apr 11, 2005
  3. globalrev
    Replies:
    4
    Views:
    756
    Gabriel Genellina
    Apr 20, 2008
  4. Alex Untitled
    Replies:
    11
    Views:
    658
    Giampiero Zanchi
    Nov 16, 2009
  5. VK
    Replies:
    15
    Views:
    1,159
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page