How to call a Perl script from Python, piping input to it?

13,853

Solution 1

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

Solution 2

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";

Solution 3

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()

Solution 4

"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.

Solution 5

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?

Share:
13,853

Related videos on Youtube

Oz.
Author by

Oz.

merge delete

Updated on April 17, 2022

Comments

  • Oz.
    Oz. about 2 years

    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

  • Oz.
    Oz. about 15 years
    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.
  • Chas. Owens
    Chas. Owens about 15 years
    popen has been deprecated since version 2.6, you should use subprocess instead.
  • nosklo
    nosklo about 15 years
    @Alex Fort: Just grab the subprocess module from 2.4 and run it on 2.3. It seems to work.
  • user1066101
    user1066101 about 15 years
    From the question: "only has python v2.3 " deprecated hasn't happened yet.
  • Ramy Mohamed
    Ramy Mohamed about 15 years
    According to the question, the Perl script should accept input, not an arg.
  • Oz.
    Oz. about 15 years
    This worked pretty well. I ended up using popen2, opening a input and output, which worked like a charm. Thanks for the help.