6

I'm hacking some support for DomainKeys and DKIM into an open source email marketing program, which uses a python script to send the actual emails via SMTP. I decided to go the quick and dirty route, and just write a perl script that accepts an email message from STDIN, signs it, then returns it signed.

What I would like to do, is from the python script, pipe the email text that's in a string to the perl script, and store the result in another variable, so I can send the email signed. I'm not exactly a python guru, however, and I can't seem to find a good way to do this. I'm pretty sure I can use something like os.system for this, but piping a variable to the perl script is something that seems to elude me.

In short: How can I pipe a variable from a python script, to a perl script, and store the result in Python?

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

6 Answers 6

11

Use subprocess. Here is the 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";
Sign up to request clarification or add additional context in comments.

1 Comment

According to the question, the Perl script should accept input, not an arg.
9

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

3 Comments

popen has been deprecated since version 2.6, you should use subprocess instead.
From the question: "only has python v2.3 " deprecated hasn't happened yet.
This worked pretty well. I ended up using popen2, opening a input and output, which worked like a charm. Thanks for the help.
7
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()

Comments

2

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

Correct. The subprocess module is like os.system, but provides the piping features you're looking for.

2 Comments

I should have clarified that I'm only working with python 2.3, and it seems the subprocess module is for 2.4 and up.
@Alex Fort: Just grab the subprocess module from 2.4 and run it on 2.3. It seems to work.
2

I'm sure there's a reason you're going down the route you've chosen, but why not just do the signing in Python?

How are you signing it? Maybe we could provide some assitance in writing a python implementation?

8 Comments

I would do the signing from python, but my python-fu is pretty weak. That would be a more optimal solution, however, so I may consider that in the future, especially if I decide to contribute the code to the community.
If you post the Perl solution somewhere I'd be happy to help try and convert it :-)
The Perl solution is really simple. I'm just taking advantage of the Mail::DKIM module, which makes signing an email pretty trivial.
So something like this should make a Python implementation similarly trivial: hewgill.com/pydkim :-)
...ah, except it requires Python >= 2.5 ...rats! Out of curiosity, why are you still using 2.3 not a more recent build?
|
1

I tried also to do that only configure how to make it work as

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

I wish this will assist u to make it work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.