Determine sockets in use by python

exarkun at exarkun at
Thu Sep 30 21:44:13 CEST 2010

On 07:32 pm, jmellander at 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
>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 
  * and os.write let you read and write directly on file 
descriptors (although it sounds like you might not need this)

>On Thu, Sep 30, 2010 at 7:14 AM, Jean-Paul Calderone
><exarkun at> wrote:
>>On Sep 29, 4:08 pm, Jim Mellander <jmellan... at> wrote:
>>>On Wed, Sep 29, 2010 at 11:05 AM, Gary Herron <gher... at> 
>>> > On 09/29/2010 09:50 AM, Jim Mellander wrote:
>>> >> Hi:
>>> >> I'm a newbie to python, although not to programming.  Briefly, I 
>>> >> using a binding to an external library used for communication in a
>>> >> client-server context, with the server in python.  Typically, I 
>>> >> set this up with event callbacks, and then enter a select loop, 
>>> >> most the time idles and processes input events when the socket 
>>> >> activity, kinda like:
>>> >> while True:
>>> >>,(),())
>>> >>     process_event()
>>> >> Unfortunately, the API does not expose the socket to the script 
>>> >> 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 
>>> >> to the script level, then the problem would be solved.
>>> >> Failing that, it would be nice to be able to pythonically 
>>> >> 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 
>>> > 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

More information about the Python-list mailing list