简体   繁体   中英

Running bash commands in python with quotes

I am trying to run a bash command to start up a stream using MJPG streamer in python. While I know the general process is to put the command in as a string, split the string, then put the split string into Popen, the issue I'm having is that the command requires double quotes and .split() removes these so I am getting errors stating that the -d flag is an unrecognised option. The command runs fine if I just run it directly, but I can't seem to get it running from python (Python 2.7).

from subprocess import Popen

def start_stream(device):
    stream_start_cmd = """
                       sudo /usr/local/bin/mjpg_streamer -i 
                       "/usr/local/lib/input_uvc.so -d /dev/video{0} -y"
                       -o "/usr/local/lib/output_http.so -w  
                       /usr/local/www -p {1}"
                       """.format(device,
                                  '80' if device == 0 else '443 &')
    Popen(stream_start_cmd.split())


if __name__ == '__main__':
    start_stream(0)

Also side note, is there any better way to format this mess?

The python document says:

args should be a sequence of program arguments or else a single string.

Based on the command you provided, once split, we have

['sudo', '/usr/local/bin/mjpg_streamer', '-i', '"/usr/local/lib/input_uvc.so', '-d', '/dev/video{0}', '-y"', '-o', '"/usr/local/lib/output_http.so', '-w', '/usr/local/www', '-p', '{1}"']

You can see there's double quote in front of /usr/local/lib/input_uvc.so and after -y . Those double quotes will make the args inaccurate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM