3

My code has two potential outcomes in the terminal: Can't connect RFCOMM socket: Permission denied and Can't connect RFCOMM socket: Host is down. I need to store either result as a string in a variable, but everything I've tried has failed. This is the code that I thought would do it:

from subprocess import check_output

out = check_output(["sudo", "rfcomm", "connect", "0", "AA:BB:CC:DD:EE:FF", "10"])
print "output: %s" % out

Instead I get nothing:

user:~/home $./foo.py
Can't connect RFCOMM socket: Permission denied
output:

Another attempt:

proc = subprocess.Popen(["sudo rfcom connect 0 AA:BB:CC:DD:EE:FF 10"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print "output: %s" % out, err

This at least gives me something when I print. Unfortunately it's "None" telling me there is no error and not the actual output:

user:~/home $./foo.py
Can't connect RFCOMM socket: Permission denied
output:  None

I've already tried this this this this and probably a couple others. I'm sure I'm missing a piece of critical knowledge somewhere. Thanks for any pointers!

Community
  • 1
  • 1
quelleck
  • 146
  • 1
  • 11

1 Answers1

3

rfcomm is apparently writing its output to standard error, yet you are only capturing standard output. To capture both, include stderr=subprocess.STDOUT in the call to check_output:

subprocess.check_output(["sudo", "rfcomm", "connect", "0", "AA:BB:CC:DD:EE:FF", "10"],
                        stderr=subprocess.STDOUT)
jwodder
  • 54,758
  • 12
  • 108
  • 124