checking that process binds a port, fuser functionality

Discussion in 'Python' started by Zdenek Maxa, Aug 2, 2010.

  1. Zdenek Maxa

    Zdenek Maxa Guest

    Hello,

    I need to start a process (using subprocess.Popen()) and wait until the
    new process either fails or successfully binds a specified port. The
    fuser command seems to be indented exactly for this purpose. Could
    anyone please provided a hint to a handy Python library to do this or
    would the advice be to parse fuser command output?

    This needs to happen on Linux and Python 2.4.

    Thanks a lot in advance.

    Zdenek
     
    Zdenek Maxa, Aug 2, 2010
    #1
    1. Advertising

  2. Zdenek Maxa

    Nobody Guest

    On Mon, 02 Aug 2010 23:27:37 +0200, Zdenek Maxa wrote:

    > I need to start a process (using subprocess.Popen()) and wait until the
    > new process either fails or successfully binds a specified port. The
    > fuser command seems to be indented exactly for this purpose. Could
    > anyone please provided a hint to a handy Python library to do this or
    > would the advice be to parse fuser command output?
    >
    > This needs to happen on Linux and Python 2.4.


    fuser (when applied to a TCP socket) scans /proc/net/tcp to obtain the
    inode number, then scans /proc/[1-9]*/fd/* for a reference to the inode.
    This requires sufficient privileges to enumerate the /proc/<pid>/fd
    directories (i.e. if you aren't running as root, fuser will ignore any
    processes which you don't own).

    If you just need to wait until *something* is listening on that port, you
    could try connect()ing to it. Alternatively, you can monitor /proc/net/tcp
    until the relevant port appears.

    If you know which process will be using the port, you can just scan the
    /proc/<pid>/fd directory for that process, rather than checking all
    processes. You still need to use /proc/net/tcp to obtain the inode number.
     
    Nobody, Aug 3, 2010
    #2
    1. Advertising

  3. Zdenek Maxa

    Roy Smith Guest

    In article <>,
    Nobody <> wrote:

    > On Mon, 02 Aug 2010 23:27:37 +0200, Zdenek Maxa wrote:
    >
    > > I need to start a process (using subprocess.Popen()) and wait until the
    > > new process either fails or successfully binds a specified port.

    >
    > If you just need to wait until *something* is listening on that port, you
    > could try connect()ing to it.


    This certainly seems like the logical way to me. It's straight-forward,
    simple, and portable.
     
    Roy Smith, Aug 3, 2010
    #3
  4. Zdenek Maxa

    Zdenek Maxa Guest

    -------- Original Message --------
    Subject: Re: checking that process binds a port, fuser functionality
    From: Roy Smith <>
    To:
    Date: Tue Aug 03 2010 13:06:27 GMT+0200 (CEST)

    > In article <>,
    > Nobody <> wrote:
    >
    >> On Mon, 02 Aug 2010 23:27:37 +0200, Zdenek Maxa wrote:
    >>
    >>> I need to start a process (using subprocess.Popen()) and wait until the
    >>> new process either fails or successfully binds a specified port.

    >> If you just need to wait until *something* is listening on that port, you
    >> could try connect()ing to it.

    >
    > This certainly seems like the logical way to me. It's straight-forward,
    > simple, and portable.


    Yes, but I need a check that certain known process's PID listens on a
    defined port.
    connect() would certainly work, but I may end up connecting to a
    different process.
    I forgot to mention that my master daemon starts processes in question
    as external
    applications, defines port they should bind but starts them via
    different user via sudo,
    which makes checking /proc/net/tcp not possible.
    Well, seems it's turning out not straight-forward, but thanks a lot for
    your thoughts anyway!

    Zdenek
     
    Zdenek Maxa, Aug 3, 2010
    #4
  5. Zdenek Maxa

    Roy Smith Guest

    On Aug 3, 10:32 am, Zdenek Maxa <> wrote:

    > Yes, but I need a check that certain known process's PID listens on a
    > defined port. connect() would certainly work, but I may end up
    > connecting to a different process.


    Then you need to define your protocol such that the client and server
    engage in some sort of identification / authentication exchange when
    they connect.

    Client: Who are you?
    Server: I am PID 12345
    Client: That's not who I was expecting, I'm going away!

    Depending on how secure you need this to be, the exchange might
    include some kind of cryptographic signature.
     
    Roy Smith, Aug 3, 2010
    #5
    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. MattB
    Replies:
    3
    Views:
    1,121
    MattB
    Sep 2, 2004
  2. subaruwrx88011
    Replies:
    4
    Views:
    631
    Alan Johnson
    May 17, 2006
  3. Lee Boozer

    GridView bound to DataSet binds to...what precisely?

    Lee Boozer, May 26, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    119
    Lee Boozer
    May 26, 2006
  4. Peter Schuller

    WEBrick always binds twice -> fails

    Peter Schuller, Feb 5, 2004, in forum: Ruby
    Replies:
    0
    Views:
    179
    Peter Schuller
    Feb 5, 2004
  5. K. Frank
    Replies:
    4
    Views:
    171
    K. Frank
    Jul 17, 2013
Loading...

Share This Page