Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Running the following python script through web site works fine and (as expected) stops the playback of MPD:

#!/usr/bin/env python
import subprocess
subprocess.call(["mpc", "stop"])
print ("Content-type: text/plain;charset=utf-8\n\n")
print("Hello") 

This script however causes an error (playback starts as expected):

#!/usr/bin/env python
print("Content-type: text/plain;charset=utf-8\n\n")
print ("Hello")

import subprocess
subprocess.call(["mpc", "play"])

The error is:

malformed header from script. Bad header=Scorpions - Eye of the tiger -: play.py, referer: http://...

Apparently whatever is returned by the playback command is taken as the header. When run in terminal, the output looks fine. Why could it be?

share|improve this question

2 Answers 2

  1. You're running your script in some sort of CGI-like environment. I would strongly suggest using a light web framework like Flask or Bottle.
  2. mpc play is writing to stdout. You need to silence it:

    import os
    
    with open(os.devnull, 'w') as dev_null:
        subprocess.call(["mpc", "stop"], stdout=dev_null)
    
  3. For your HTTP headers to be valid, you need to separate them with \r\n, not \n\n.
share|improve this answer
    
1. I will likely use some framework, this are early tries only. 2. Redirecting stdout did help, but that does not explain, why stop works fine and play needs special measures like that (both write to stdout) 3. that does not change the behaviour, but I will –  Lukas Jan 11 at 22:17
    
@Lukas: Terminate your last header with \r\n\r\n instead of just \r\n. –  Blender Jan 11 at 22:56
    
I use \r\n\r\n now, still it does not work without redirecting stdout –  Lukas Jan 11 at 23:16
    
@Lukas: The first or the second? –  Blender Jan 11 at 23:21
    
first (stop) works from the beginning as is written in the question. The second one (play) works only with stdout redirected. What is used to terminate seems not to be important. –  Lukas Jan 11 at 23:32

You need to use \r\n line endings.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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