Re: Determine sockets in use by python

Discussion in 'Python' started by Jim Mellander, Sep 29, 2010.

  1. Hi Gary:

    Certainly not windows.... I'm developing on OS/X but for production
    probably Linux and FreeBSD

    (I'm hoping for something a bit more portable than running 'lsof' and
    parsing the output, but appreciate any/all advice)

    On Wed, Sep 29, 2010 at 11:05 AM, Gary Herron <> wrote:
    > On 09/29/2010 09:50 AM, Jim Mellander wrote:
    >>
    >> Hi:
    >>
    >> I'm a newbie to python, although not to programming.  Briefly, I am
    >> using a binding to an external library used for communication in a
    >> client-server context, with the server in python.  Typically, I would
    >> set this up with event callbacks, and then enter a select loop, which,
    >> most the time idles and processes input events when the socket shows
    >> activity, kinda like:
    >>
    >> while True:
    >>     socket.select((my_socket),(),())
    >>     process_event()
    >>
    >> Unfortunately, the API does not expose the socket to the script level,
    >> and the developer recommends a busy loop:
    >>
    >> while True:
    >>     sleep(1)
    >>     process_event()
    >>
    >> which I hope to avoid, for many reasons.  If the socket can be exposed
    >> to the script level, then the problem would be solved.
    >>
    >> Failing that, it would be nice to be able to pythonically determine
    >> the sockets in use and select on those.  Does anyone have any
    >> suggestions on how to proceed?
    >>
    >> Thanks in advance
    >>

    >
    > It's certain that any answer to this will depend on which operating system
    > you are using.  So do tell: What OS?
    >
    > --
    > Gary Herron, PhD.
    > Department of Computer Science
    > DigiPen Institute of Technology
    > (425) 895-4418
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Jim Mellander, Sep 29, 2010
    #1
    1. Advertising

  2. On Sep 29, 4:08 pm, Jim Mellander <> wrote:
    >
    > On Wed, Sep 29, 2010 at 11:05 AM, Gary Herron <> wrote:
    > > On 09/29/2010 09:50 AM, Jim Mellander wrote:

    >
    > >> Hi:

    >
    > >> I'm a newbie to python, although not to programming.  Briefly, I am
    > >> using a binding to an external library used for communication in a
    > >> client-server context, with the server in python.  Typically, I would
    > >> set this up with event callbacks, and then enter a select loop, which,
    > >> most the time idles and processes input events when the socket shows
    > >> activity, kinda like:

    >
    > >> while True:
    > >>     socket.select((my_socket),(),())
    > >>     process_event()

    >
    > >> Unfortunately, the API does not expose the socket to the script level,
    > >> and the developer recommends a busy loop:

    >
    > >> while True:
    > >>     sleep(1)
    > >>     process_event()

    >
    > >> which I hope to avoid, for many reasons.  If the socket can be exposed
    > >> to the script level, then the problem would be solved.

    >
    > >> Failing that, it would be nice to be able to pythonically determine
    > >> the sockets in use and select on those.  Does anyone have any
    > >> suggestions on how to proceed?

    >
    > >> Thanks in advance

    >
    > > It's certain that any answer to this will depend on which operating system
    > > you are using.  So do tell: What OS?

    >
    > Hi Gary:
    >
    > Certainly not windows....  I'm developing on OS/X but for production
    > probably Linux and FreeBSD
    >
    > (I'm hoping for something a bit more portable than running 'lsof' and
    > parsing the output, but appreciate any/all advice)
    >


    Linux has /proc/self/fd and OS X has /dev/fd. Those both suppose you
    have some way of determining which file descriptor corresponds to the
    socket or sockets that the library is using, of course. Vastly better
    would be to convince the author to expose that information via a real
    API.

    Jean-Paul
     
    Jean-Paul Calderone, Sep 30, 2010
    #2
    1. Advertising

  3. Thanks, I realized that even if I found out relevant info on the
    socket, I would probably need to use ctypes to provide a low level
    interface to select, as the socket wouldn't be a python socket object,
    unless there is some way to promote a c socket to a python socket
    object.

    Appreciate the info, folks.

    On Thu, Sep 30, 2010 at 7:14 AM, Jean-Paul Calderone
    <> wrote:
    > On Sep 29, 4:08 pm, Jim Mellander <> wrote:
    >>
    >> On Wed, Sep 29, 2010 at 11:05 AM, Gary Herron <> wrote:
    >> > On 09/29/2010 09:50 AM, Jim Mellander wrote:

    >>
    >> >> Hi:

    >>
    >> >> I'm a newbie to python, although not to programming.  Briefly, I am
    >> >> using a binding to an external library used for communication in a
    >> >> client-server context, with the server in python.  Typically, I would
    >> >> set this up with event callbacks, and then enter a select loop, which,
    >> >> most the time idles and processes input events when the socket shows
    >> >> activity, kinda like:

    >>
    >> >> while True:
    >> >>     socket.select((my_socket),(),())
    >> >>     process_event()

    >>
    >> >> Unfortunately, the API does not expose the socket to the script level,
    >> >> and the developer recommends a busy loop:

    >>
    >> >> while True:
    >> >>     sleep(1)
    >> >>     process_event()

    >>
    >> >> which I hope to avoid, for many reasons.  If the socket can be exposed
    >> >> to the script level, then the problem would be solved.

    >>
    >> >> Failing that, it would be nice to be able to pythonically determine
    >> >> the sockets in use and select on those.  Does anyone have any
    >> >> suggestions on how to proceed?

    >>
    >> >> Thanks in advance

    >>
    >> > It's certain that any answer to this will depend on which operating system
    >> > you are using.  So do tell: What OS?

    >>
    >> Hi Gary:
    >>
    >> Certainly not windows....  I'm developing on OS/X but for production
    >> probably Linux and FreeBSD
    >>
    >> (I'm hoping for something a bit more portable than running 'lsof' and
    >> parsing the output, but appreciate any/all advice)
    >>

    >
    > Linux has /proc/self/fd and OS X has /dev/fd.  Those both suppose you
    > have some way of determining which file descriptor corresponds to the
    > socket or sockets that the library is using, of course.  Vastly better
    > would be to convince the author to expose that information via a real
    > API.
    >
    > Jean-Paul
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Jim Mellander, Sep 30, 2010
    #3
  4. Jim Mellander

    Guest

    On 07:32 pm, wrote:
    >Thanks, I realized that even if I found out relevant info on the
    >socket, I would probably need to use ctypes to provide a low level
    >interface to select, as the socket wouldn't be a python socket object,
    >unless there is some way to promote a c socket to a python socket
    >object.
    >
    >Appreciate the info, folks.


    There are a few options to help with that part of it:

    * select() works with integer file descriptors
    * socket.socket.fromfd gives you a socket object from an integer file
    descriptor
    * os.read and os.write let you read and write directly on file
    descriptors (although it sounds like you might not need this)

    Jean-Paul
    >On Thu, Sep 30, 2010 at 7:14 AM, Jean-Paul Calderone
    ><> wrote:
    >>On Sep 29, 4:08 pm, Jim Mellander <> wrote:
    >>>
    >>>On Wed, Sep 29, 2010 at 11:05 AM, Gary Herron <>
    >>>wrote:
    >>> > On 09/29/2010 09:50 AM, Jim Mellander wrote:
    >>>
    >>> >> Hi:
    >>>
    >>> >> I'm a newbie to python, although not to programming.  Briefly, I
    >>>am
    >>> >> using a binding to an external library used for communication in a
    >>> >> client-server context, with the server in python.  Typically, I
    >>>would
    >>> >> set this up with event callbacks, and then enter a select loop,
    >>>which,
    >>> >> most the time idles and processes input events when the socket
    >>>shows
    >>> >> activity, kinda like:
    >>>
    >>> >> while True:
    >>> >>     socket.select((my_socket),(),())
    >>> >>     process_event()
    >>>
    >>> >> Unfortunately, the API does not expose the socket to the script
    >>>level,
    >>> >> and the developer recommends a busy loop:
    >>>
    >>> >> while True:
    >>> >>     sleep(1)
    >>> >>     process_event()
    >>>
    >>> >> which I hope to avoid, for many reasons.  If the socket can be
    >>>exposed
    >>> >> to the script level, then the problem would be solved.
    >>>
    >>> >> Failing that, it would be nice to be able to pythonically
    >>>determine
    >>> >> the sockets in use and select on those.  Does anyone have any
    >>> >> suggestions on how to proceed?
    >>>
    >>> >> Thanks in advance
    >>>
    >>> > It's certain that any answer to this will depend on which operating
    >>>system
    >>> > you are using.  So do tell: What OS?
    >>>
    >>>Hi Gary:
    >>>
    >>>Certainly not windows....  I'm developing on OS/X but for production
    >>>probably Linux and FreeBSD
    >>>
    >>>(I'm hoping for something a bit more portable than running 'lsof' and
    >>>parsing the output, but appreciate any/all advice)

    >>
    >>Linux has /proc/self/fd and OS X has /dev/fd.  Those both suppose you
    >>have some way of determining which file descriptor corresponds to the
    >>socket or sockets that the library is using, of course.  Vastly better
    >>would be to convince the author to expose that information via a real
    >>API.
    >>
    >>Jean-Paul
    >>--
    >>http://mail.python.org/mailman/listinfo/python-list

    >
     
    , Sep 30, 2010
    #4
  5. On Thu, 30 Sep 2010 12:32:45 -0700
    Jim Mellander <> wrote:
    > Thanks, I realized that even if I found out relevant info on the
    > socket, I would probably need to use ctypes to provide a low level
    > interface to select, as the socket wouldn't be a python socket object,
    > unless there is some way to promote a c socket to a python socket
    > object.
    >
    > Appreciate the info, folks.


    You can also try to monkeypatch the socket module (before importing the
    3rd-party library) and replace the socket.socket() constructor with a
    custom one which tracks all created sockets in a structure of your
    choice.

    Not very pretty of course.
     
    Antoine Pitrou, Oct 1, 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. billiejoex

    Closing programs that use sockets

    billiejoex, Aug 26, 2005, in forum: Python
    Replies:
    2
    Views:
    315
    billiejoex
    Aug 27, 2005
  2. Replies:
    2
    Views:
    325
    Gianni Mariani
    Oct 31, 2006
  3. Jim Mellander

    Determine sockets in use by python

    Jim Mellander, Sep 29, 2010, in forum: Python
    Replies:
    0
    Views:
    218
    Jim Mellander
    Sep 29, 2010
  4. roni
    Replies:
    0
    Views:
    120
  5. Peña, Botp
    Replies:
    1
    Views:
    246
    Robert Klemme
    Jan 24, 2004
Loading...

Share This Page