0

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?

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

5 Comments

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: Terminate your last header with \r\n\r\n instead of just \r\n.
I use \r\n\r\n now, still it does not work without redirecting stdout
@Lukas: The first or the second?
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.
0

You need to use \r\n line endings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.