[Tutor] os.popen doesn't give up

Bob Gailer bgailer at alum.rpi.edu
Thu Dec 2 17:53:27 CET 2004


At 09:37 AM 12/2/2004, Ben Vinger wrote:
>I call net-snmp from a Python script as below.  The
>script works fine and runs every 5 minutes.  But after
>collecting data for a few weeks, I noticed many
>instances of the script, as well as the snmpget
>executable in the process table.  I think this must be
>due to times when the remote site was not available,
>so would like to know how I can get os.popen and
>snmpget not to do this (ie, give it a rest when the
>remote site doesn't respond)
>
>
>I = os.popen(r'snmpget -Os -c ' + SNMPcommunity + ' -v
>2c -r 3 ' + IP + ' ' + counter, 'r')

 From the os module reference "popen( command[, mode[, bufsize]])
Open a pipe to or from command. The return value is an open file object 
connected to the pipe, which can be read or written depending on whether 
mode is 'r' (default) or 'w'

That explains it. l "is an open file object connected to the pipe, which 
can be read". snmpget stays in execution waiting for its output to be read.

Solutions:
1 - os.popen(r'snmpget -Os -c ' + SNMPcommunity + ' -v2c -r 3 ' + IP + ' ' 
+ counter, 'r'). Discards (therefore closes) the file object.
2 - l = os.popen(r'snmpget -Os -c ' + SNMPcommunity + ' -v2c -r 3 ' + IP + 
' ' + counter, 'r').read() Puts snmpget's output in l.
3 - l = os.popen(r'snmpget -Os -c ' + SNMPcommunity + ' -v2c -r 3 ' + IP + 
' ' + counter, 'r')
      l.close()
Enough?

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20041202/8bf481fb/attachment.html


More information about the Tutor mailing list