[Tutor] [unclassified] Re: Pipe variable to external command (fwd)
Jeffrey Rice
jeffrice at finity.org
Thu May 12 23:22:11 CEST 2005
At 12:24 PM 5/12/2005, Danny Yoo wrote:
>######
> >>> import popen2
> >>> process = popen2.Popen3(['wc'])
> >>> process.tochild.write("hello\nworld")
> >>> process.tochild.close()
> >>> process.fromchild.read()
>' 1 2 11\n'
> >>> process.fromchild.close()
> >>> process.wait()
>0
>######
This is all well and good if the child exits with 0, but things get a
little funky if it doesn't.
For example, modifying the model to have clamdscan exit with 0:
######
>>> import popen2
>>> process = popen2.Popen3(['clamdscan','ham.test'])
>>> process.tochild.close()
>>> process.fromchild.read()
'ham.test: OK\n\n----------- SCAN SUMMARY -----------\nInfected files:
0\nTime: 0.098 sec (0 m 0 s)\n'
>>> process.fromchild.close()
>>> process.wait()
0
######
Now rewrite it to return an exit code of 1:
######
>>> import popen2
>>> process = popen2.Popen3(['clamdscan','eicar.test'])
>>> process.tochild.write("hello\nworld")
>>> process.tochild.close()
>>> process.fromchild.read()
'eicar.test: Eicar-Test-Signature FOUND\n\n----------- SCAN SUMMARY
-----------\nInfected files: 1\nTime: 0.043 sec (0 m 0 s)\n'
>>> process.fromchild.close()
>>> process.wait()
256
######
Now for a exit code of 2:
######
>>> import popen2
>>> process = popen2.Popen3(['clamdscan','nonexistent.test'])
>>> process.tochild.close()
>>> process.fromchild.read()
'\n----------- SCAN SUMMARY -----------\nInfected files: 0\nTime: 0.004 sec
(0 m 0 s)\n'
>>> process.fromchild.close()
>>> process.wait()
512
######
In my fiirst example, clamdscan should return 1 when the EICAR string is
found -- process.wait() gets 256 instead.
In the second, clamdscan returns 2 if an error occurs, such as trying to
scan a non-existent file, but 512 is returned.
No doubt there is a reason why the exit code is getting multiplied by
256... ??? My search has not enlightened me, however.
Jeff
* * * * * * *
Jeffrey Rice || jeffrice at finity.org || www.finity.org
More information about the Tutor
mailing list