Glueing applications with Python

The problem I faced was the following:

  • Two processes I have to communicate with in order to validate data using stdin/stdout
  • No source code of this programs
  • Windows Platform

Since I am a UNIX guy I haven’t ever done anything advanced in Windows so I didn’t have the time to learn how to achive this in Windows as I would have done in any other Unix. Obviously I downloaded Mingw and I tried to used the proper POSIX functions but it didn’t work.

Now is when Python comes into play. It is an “easy to learn” (at sometime I will be discussing this topic) scripting language with a pretty well support in Windows platform, at some point this support seemed better than perl’s one so I chose Python.

After googling for 2 minutes about basic Python syntax and looking for function descriptions I came up with the solution, which was really straightforward and worked super-duper.

import subprocess

program = subprocess.Popen([‘C:\path\to\program.exe, arg1, argn’], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE);

ret = check_ad.communicate(username + ” ” + group[0])

ret = program.communicate(“writing to stdin”);

print ret

Read from stdout

Really easy and simple code that can save someones life when working under pressure and do not have time to read a dozen of C/C++ examples.