Unsupported operand type(s) for +: 'Popen' and 'str'' in subprocess.Popen

852 Views Asked by At

I'm looking at this script to disable shell=True while creating an ova.

It works when shell=True but gives me a TypeError when shell=False. I'm pretty sure the issue is with this part of the command - " vi:\//" + username + ":" + encodedPassword + "@" + hostname" - because when I remove this element from the list (i.e. command), the script moves forward. I've tried hardcoding values like "vi:\//user:[email protected]" and also removed so but I still get this type error.

Python version is 2.7

com1 = "/usr/bin/ovftool --acceptAllEulas --disableVerification --noSSLVerify --datastore=" + datastore +
           " --network=\"" + network + "\" --name=" + name + " " + ovalocation + " vi:\//" +  username +
          ":" + encodedPassword + "@" + hostname

#prior to disabling shell=True
# coms = subprocess.Popen(com1, shell=True,
  #                     stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# set shell=False - now it requires the command to be a list rather than a string.
coms = subprocess.Popen(com1.split(), shell=False,
                       stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Error:

'unsupported operand type(s) for +: 'Popen' and 'str''

What am I doing wrong?

Edit: I know it's an issue with the vi command becuase when replace "vi:\//" + username + ":" + encodedPassword + "@" + hostname" with a hardcoded "hello" the script moves forward.

1

There are 1 best solutions below

3
On

If you are using a lot of argument's, it might be easier using a string instead of a list of arguments which you can do by setting shell=True. That's easy to debug as well, as the same command (which you should print) will run from a native shell in the same fashion.

coms = subprocess.Popen(com1, shell=True,
                   stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Just keep in mind, that shell=True might have security issues depending on how this is being run.