Map port to process

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

    Map port to process

    Is there a way in python to figure out which process is running on
    which port? I know in Windows XP you can run "netstat -o" and see the
    process ID for each open port....but I am looking for something not
    tied to windows particularly, hopefully something in python.

    if not, any known way, such as netstat shown above, for other versions
    of windows (such as 98, 2000) and even linux?

    thanks

  • Diez B. Roggisch

    #2
    Re: Map port to process

    py wrote:
    [color=blue]
    > Is there a way in python to figure out which process is running on
    > which port? I know in Windows XP you can run "netstat -o" and see the
    > process ID for each open port....but I am looking for something not
    > tied to windows particularly, hopefully something in python.
    >
    > if not, any known way, such as netstat shown above, for other versions
    > of windows (such as 98, 2000) and even linux?[/color]

    Linux also knows netstat.

    And at least with on-board mechanisms (meaning the standard lib), I fear you
    can only try and use netstat as subprocess.

    I'm not aware of an extension module that does what you want, but I didn't
    google for it.

    Diez

    Comment

    • Mike Meyer

      #3
      Re: Map port to process

      "py" <codecraig@gmai l.com> writes:[color=blue]
      > Is there a way in python to figure out which process is running on
      > which port? I know in Windows XP you can run "netstat -o" and see the
      > process ID for each open port....but I am looking for something not
      > tied to windows particularly, hopefully something in python.[/color]

      Well, once you get to "no tied to an OS", you're out of luck. You can
      run network stacks on systems that don't have processes, so there's no
      general mechanism for getting process information from a
      socket. Restricting things to posixish systems doesn't seem to help
      much, as there doesn't seem to be a posix interface to get it either.
      [color=blue]
      > if not, any known way, such as netstat shown above, for other versions
      > of windows (such as 98, 2000) and even linux?[/color]

      Many windows networking tools started life as Unix networking
      tools. Netstat is one of them. The -o flag may be a Windows addition,
      though. FreeBSD (for instance) doesn't have it, but provides the
      sockstat command to get pid/process name/etc. info on
      sockets. Sockstat uses sysctls to pull specific kernel structures,
      which means it's OS-specific. Python doesn't seem to have a way to do
      sysctl calls, either.

      <mike
      --
      Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
      Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

      Comment

      Working...