1

I am trying to use the subprocess to execute a command socat. It just echos the command as 'show stat | socat unix-connect:/users/viperias/Applications/haproxy.stat stdio' rather than printing the output of the command. It perfectly works for the other commands. Am I missing something?

    def main():
        cmd = "echo show stat | socat unix-connect:/users/viperias/Applications/haproxy.stat stdio"
        ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
        output = ps.communicate()[0]
        print (output)
    main()
  • One thing to note is that you should be using `subprocess.Popen(cmd.split() ...` – Reut Sharabani Mar 29 '15 at 08:00
  • possible duplicate of [link several Popen commands with pipes](http://stackoverflow.com/questions/7389662/link-several-popen-commands-with-pipes) – Reut Sharabani Mar 29 '15 at 08:05
  • The behaviour of the command depends on your shell - on mine I get "socat: not found", aka the pipe was correctly parsed. To not depend on the shell used see the duplicate suggested by Reut – Antti Haapala -- Слава Україні Mar 29 '15 at 09:46
  • For something as simple as this, I would really recommend to use the python socket module to connect to a local [unix domain socket](http://pymotw.com/2/socket/uds.html): – Christian Eichelmann Mar 29 '15 at 10:52
  • possible duplicate of [How do I use subprocess.Popen to connect multiple processes by pipes?](http://stackoverflow.com/questions/295459/how-do-i-use-subprocess-popen-to-connect-multiple-processes-by-pipes) – Roland Smith Mar 29 '15 at 15:42

0 Answers0