[Tutor] Pipe variable to external command (solved)

Jeffrey Rice jeffrice at finity.org
Sun May 15 20:41:37 CEST 2005


At 04:17 PM 5/13/2005, Alan Gauld wrote:
>AS a general pont all uppercase usually means a consrtant in Python.
>Variable names typically start lowercase and either use capital
>letters to separate words likeThis, or use underscores like_this.
>
>Variables starting with an uppercase letter tend to indicate class
>names - although thats not universally true, even in the standard
>library! But the ALLUPPER thing for constants is pretty universal
>so its worth adopting IMHO.

Thanks for the style pointers.  I will make these changes -- I realize how 
important consistency is, since I use other people's code as a learning 
tool with great frequency.

>So what happens? Do you get any error messages?
>What value is CLAM_RESULT showing?
>What value are you expecting? (The actual return value from
>popen/close is not clearly defined in general.)
>Finally, although you close the pipe it may be worth trying
>a flush() first just to clear the buffer.

I have gotten this working, with much help (especially from Danny).  Here 
is what the code I settled on looks like:

###
working = email.message_from_file(open(emailfile))

clamav = popen2.Popen3(clamav_cmd,1)
clamav.tochild.write(working.as_string())
clamav.tochild.close()
clamav_result = clamav.fromchild.read().lstrip('stream: ')
clamav.fromchild.close
clamav_exit = os.WEXITSTATUS(clamav.wait())
###

This gives me a string (clamav_result) that contains the output from the 
scanner:  either OK if it was clean, or the name of the virus 
detected.  (it is prefixed with "stream: " to indicate that stdin was 
scanned, so I strip that off)
clamav_exit contains the exit code, converted using WEXITSTATUS:  either 0, 
1, or 2 for OK, infected, or error respectively.

This seems to be working out just fine, and gives me a lot more flexibility 
that the bash script I was using!  (as a note, I am reading the email into 
an email object because I perform other actions, like adding 
headers.  Otherwise, it would be simpler to have clamav read the file 
directly rather than the stream.)

Thanks to everyone for your patience and advice!

Jeff

*       *       *       *       *       *       *
Jeffrey Rice    ||     jeffrice at finity.org      ||  www.finity.org



More information about the Tutor mailing list