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:
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.)