2011-04-13 3 views
3

하위 프로세스를 사용하여 sox (오디오 처리 프로그램)를 호출하는 Python CGI 스크립트를 작성하려고합니다. 문제는 sox 호출에서 오류가 발생하면 모든 것이 충돌하고 Apache에서 "잘못된 헤더"오류가 발생한다는 것입니다.파이썬 CGI 스크립트에서 하위 프로세스의 오류를 어떻게 잡을 수 있습니까?

관련 비트 :

def downsample(in_file, in_location, sox, out_location): 
    """ run sox """ 
... 
    sox = shlex.split(sox) 
    retcode = subprocess.check_call(sox) 
    if not retcode == 0: 
    print '<b>something went wrong in sox: returned error code ' +\ 
      retcode + ', but we are continuing anyway...</b>' 
    """p = subprocess.Popen(sox) 
    if p.stdout: 
    print '<b>something happened in sox: returned ' +\ 
      p.stdout.read() + ', but we will keep going...</b>' 
    if p.stderr: 
    print '<b>something happened in sox: returned ' +\ 
      p.stderr.read() + ', but we will keep going...</b>'""" 
... 

def main(): 
    print "Content-Type: text/html\n\n" 

... 
    downsample(in_file, in_location, command, out_location) 
... 

if __name__ == '__main__': 
    main() 

나는 (500 페이지를 피하기 위해) 지금 스택 추적을 인쇄 할 CGI 오류 처리기를 허용하도록 check_call을 사용하고 있습니다,하지만 난 정말 잡을 싶습니다 오류, 직접 처리 및 스크립트로 이동하십시오. try : calledProcessError : 문을 제외하고 check_call을 래핑하여 시도했지만 500 페이지로 다시 넘어갔습니다. 주석 처리 된 부분도 저에게 효과적이지 않습니다.

그리고는/var/www가에서/아파치/error_log는 : 그것은 전에 헤더 인쇄를 삭스 명령 을 실행하는 것 같다 왜 이해할 수 없다

Wed Apr 13 10:08:21 2011] [error] [client ::1] sox FAIL formats: can't open input file `/tmp/wavs/haha/f.wav': WAVE: RIFF header not found, referer: http://localhost/~Valkyrie_savage/ 
[Wed Apr 13 10:08:21 2011] [error] [client ::1] malformed header from script. Bad header=\x1f\x8b\b: downsample.py, referer: http://localhost/~Valkyrie_savage/ 

. 그렇지 않은 경우 헤더의 형식이 잘못되었다고 말하는 이유는 무엇입니까?

답변

1

버퍼링 된 출력을 사용하는 것처럼 들립니다.

그런 경우 명령의 출력이 이고 HTTP 헤더가이되기 전에 출력되므로 웹 브라우저가 혼란 스럽습니다.

시스템 호출을하기 전에 sys.stdout.flush()으로 전화하여 출력 버퍼의 모든 헤더와 html이 실제로 인쇄되는지 확인할 수 있습니다.

희망이 도움이됩니다.

+0

고마워요! 그것은 매력처럼 작용했습니다. –

0

subprocess.check_call을 사용하고 CalledProcessError 예외를 잡을 수 있습니다.

+0

나는 그것을 시도했다. 실제로 그것은 또한 500 페이지 (다시 잘못된 헤더 오류)가되었다. –

2
import subprocess 
p = subprocess.Popen(cmd) 
    p.wait() 
    if p.returncode: 
     print "failed with code: %s" % str(p.returncode) 
관련 문제