How to call a Perl script from Python, the pipeline included in it?

I am hacking some support for DomainKeys and DKIM into an open source email program that uses a python script to send actual emails through SMTP. I decided to take a quick and dirty route and just write a perl script that receives an email from STDIN, signs it, and then returns it signed.

What I would like to do is from a python script, send the email text that is in the line to the perl script, and save the result in another variable so that I can send a signed letter. However, I am not quite a python guru, and I cannot find a good way to do this. I'm sure I can use something like os.system for this, but forwarding a variable to a perl script is something that seems to elude me.

In short: how can I pass a variable from a python script to a perl script and save the result in Python?

EDIT: I forgot to include that the system I'm working with only has python v2.3

+6
python perl dkim
source share
6 answers

os.popen () will return a tuple with the stdin and stdout subprocess.

+9
source share

Use subprocess . Here is a Python script:

 #!/usr/bin/python import subprocess var = "world" pipe = subprocess.Popen(["./x.pl", var], stdout=subprocess.PIPE) result = pipe.stdout.read() print result 

And here is the Perl script:

 #!/usr/bin/perl use strict; use warnings; my $name = shift; print "Hello $name!\n"; 
+10
source share
 from subprocess import Popen, PIPE p = Popen(['./foo.pl'], stdin=PIPE, stdout=PIPE) p.stdin.write(the_input) p.stdin.close() the_output = p.stdout.read() 
+6
source share

"I'm pretty sure I can use something like os.system for this, but forwarding a variable to a perl script is something that seems to elude me."

Correctly. The subprocess module is similar to os.system, but provides the pipeline functions you are looking for.

+2
source share

I'm sure there is a reason why you chose a route, but why not just subscribe to Python?

How do you sign it? Maybe we could give some help in writing a python implementation?

+2
source share

I also tried to do this only by configuring how to make it work as

 pipe = subprocess.Popen( ['someperlfile.perl', 'param(s)'], stdin=subprocess.PIPE ) response = pipe.communicate()[0] 

I want this to help you get it to work.

+1
source share

All Articles