Checking if port is in use.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Polite

    #1

    Checking if port is in use.


    If I try to bind a socket to a port that's already in use I get this
    error

    "error socket.error: (98, 'Address already in use')"

    Is there anyway to check in advance if a port i already taken?

    alex

    --
    Alex Polite

  • Peter Hansen

    #2
    Re: Checking if port is in use.

    Alex Polite wrote:[color=blue]
    > If I try to bind a socket to a port that's already in use I get this
    > error
    >
    > "error socket.error: (98, 'Address already in use')"
    >
    > Is there anyway to check in advance if a port i already taken?[/color]

    In general in Python it's not considered good style to
    "look before you leap". After all, in cases like you
    describe, some other could easily grab that port between
    the time you discover that it's available and the time
    you try to actually open the port.

    There are also probably alternative approaches to what
    you are trying to do, assuming it's not the case that all
    you're doing is trying to avoid the exception. (If that's
    the case, just catch the exception: that's how you do it
    in Python.)

    You could, for example, bind to a port of "0" and that will
    auto-assign an available port for you. Does that work
    in your case? If not, please describe what you are really
    trying to accomplish.

    -Peter

    Comment

    • Alex Polite

      #3
      Re: Checking if port is in use.

      On lör, mar 19, 2005 at 10:12:10 -0500, Peter Hansen wrote:[color=blue]
      > Alex Polite wrote:
      >
      > You could, for example, bind to a port of "0" and that will
      > auto-assign an available port for you. Does that work
      > in your case? If not, please describe what you are really
      > trying to accomplish.[/color]

      I'm launching another server (non python) from python and I have to
      designate which port that server should bind to.

      alex

      --
      Alex Polite

      Comment

      • elbertlev@hotmail.com

        #4
        Re: Checking if port is in use.

        How about this?

        try:
        s.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR,1)
        s.bind((HOST, PORT))
        except socket.error, e:
        if e....
        print "address already in use"

        Comment

        • Grant Edwards

          #5
          Re: Checking if port is in use.

          On 2005-03-19, Alex Polite <m4@polite.se > wrote:[color=blue]
          >
          > If I try to bind a socket to a port that's already in use I get this
          > error
          >
          > "error socket.error: (98, 'Address already in use')"
          >
          > Is there anyway to check in advance if a port i already taken?[/color]

          Yes. Try to bind to the port. If you get teh "Address already
          in use" error, then it's already taken.

          --
          Grant Edwards grante Yow! Is this BOISE??
          at
          visi.com

          Comment

          • Peter Hansen

            #6
            Re: Checking if port is in use.

            Alex Polite wrote:[color=blue]
            > On lör, mar 19, 2005 at 10:12:10 -0500, Peter Hansen wrote:[color=green]
            >>Alex Polite wrote:
            >>
            >>You could, for example, bind to a port of "0" and that will
            >>auto-assign an available port for you. Does that work
            >>in your case? If not, please describe what you are really
            >>trying to accomplish.[/color]
            >
            > I'm launching another server (non python) from python and I have to
            > designate which port that server should bind to.[/color]

            If I understand correctly, then you have only two
            clear options:

            1. Launch the server on whatever port, and make sure you
            can identify its failure when that port is in use.
            Presumably then you can just step your way through the
            available ports until one of them "takes".

            2. Check the port ahead of time, using the pattern of
            "bind to port, catch exception if in use, release port if
            not in use", then basically do #1, noting that if you
            can't catch the server failing you will still have the
            slight possibility that something else could grab the
            port in the meantime. I suspect that's unlikely to happen
            in your case, so you can likely ignore that.

            -Peter

            Comment

            Working...