checking that process binds a port, fuser functionality

Nobody nobody at nowhere.com
Tue Aug 3 04:47:55 EDT 2010


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.




More information about the Python-list mailing list