I need to port my Perl log handling script to Python. Here is a simple example of a Perl script I can use:

#!/usr/local/bin/perl

$|=1; # Use unbuffered output

while(<>) {
   system("beep");
}

As you can probably see, I am telling the system to beep whenever a request is made to test the script. Everything works fine on this one, but when I try a Python script such as this:

import sys
import os

for line in sys.stdin:
    os.system('beep')

everything runs but the system does not beep after a request. Here are the lines I was and am using in my apache configuration file:

CustomLog "|perl /var/web/onhit.pl" "onhit"   <-OLD LINE
CustomLog "|python /var/web/onhit.py" "onhit" <-NEW LINE

I am following this email on the Python mailing list. Anyone have an idea why this is not working?

EDIT: I know the problem has something to do with "for line in sys.stdin". For some reason it is just not detecting anything in stdin. I have no idea whether it is my python script or my apache configuration causing this, though.

link|improve this question

57% accept rate
If your problem is related to the actual script you might find you get better answers on stackoverflow – Zoredache Aug 20 '09 at 2:52
feedback

2 Answers

up vote 1 down vote accepted

Where are you getting beep from? Have you tried putting in the full path to this beep command?

Edited to add code sample.

Last time I hacked something in python to read stdin as a pipe my code looked like this.

#!/usr/bin/python
import sys

while 1:
  line = sys.stdin.readline()
  if not line:
    break
  else:
    print >> sys.stdout,  'got: %s' % line.strip()
    sys.stdout.flush()
link|improve this answer
Beep is a command on my machine that makes the system beep. It is in the path variable so I do not need to list the full path. I know it is not that line giving me the trouble; that is just a test to see if things are working. – Cory Walker Aug 20 '09 at 2:04
This sounds like the most likely reason. – Brad Gilbert Aug 20 '09 at 6:13
Thanks a lot, i guess the guy on the mailing list didn't know what he was talking about. It makes sense that the stdin test should be in a while, and not a for loop. – Cory Walker Aug 20 '09 at 14:27
feedback

Low hanging fruit: paths, permissions, ownerships.

link|improve this answer
Well it doesn't really matter, but onhit.py has execute permissions, and I know all the paths are right. Ownerships are both root. – Cory Walker Aug 20 '09 at 2:49
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.