Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python subprocess Popen argument issue not parsing full string?

I am using subprocess.Popen to start a process with a argument. However, it is cutting off the argument and driving me crazy! lol.

randport = random.randint(3000, 7000)
args='-p '+str(randport)+' --base-path /wd/hub'
subprocess.Popen(['appium', args], cwd="C:\\Users\\Computer", shell=True)

Example of what is actually being executed:
Let’s say randport got generated to be 3333

subprocess.Popen(['appium', '-p 3333'], cwd="C:\\Users\\Computer", shell=True)

What should of been executed:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

subprocess.Popen(['appium', '-p 3333 --base-path /wd/hub'], cwd="C:\\Users\\Computer", shell=True)

>Solution :

When passing arguments as a list, you have to pass every single space-separated argument separately:

subprocess.Popen(['appium', '-p', '3333', '--base-path', '/wd/hub'], cwd="C:\\Users\\Computer", shell=True)\

This is to prevent shell injection. On the other hand, you’re using shell=True. You almost certainly don’t want that, but if you do, you can simply pass a string instead of a list, since you’re vulnerable to shell injection anyhow.

(If your command is a constant string like this, injection protection is moot.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading