pythonsubprocessufw

Bad Port error on ufw command from Python subprocess


I'm working on a Python script that has to install some requirements into the computer, and I do it using subprocess, like so:

firewall_apache = subprocess.Popen(["sudo", "ufw", "allow", "\"Apache Full\""], stdout=subprocess.PIPE,universal_newlines=True)
for line in iter(firewall_apache.stdout.readline, ""):
    print(line)

firewall_apache.communicate()
if firewall_apache.returncode != 0:
    raise Exception

It works fine with other requirements, but not with the ufw.

If normally executed into the cmd (sudo ufw allow "Apache Full"), it works. But when executed from the subprocess, I receive: ERROR: Bad Port.

sudo ufw app list result is:

Available applications:
  Apache
  Apache Full
  Apache Secure
  CUPS

Am I using the wrong subprocess method? What am I missing? Thanks in advance!


Solution

  • I solved by simply removing the double quotes inside the last parameter.

    firewall_apache = subprocess.Popen(["sudo", "ufw", "allow", "Apache Full"], stdout=subprocess.PIPE,universal_newlines=True)
    

    Looks like Python already converts it as a string if it has a blankspace.