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

前端 未结 6 596
南方客
南方客 2021-01-01 01:21

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

相关标签:
6条回答
  • 2021-01-01 01:45

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

    0 讨论(0)
  • 2021-01-01 01:45
    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()
    
    0 讨论(0)
  • 2021-01-01 01:54

    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?

    0 讨论(0)
  • 2021-01-01 01:55

    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";
    
    0 讨论(0)
  • 2021-01-01 01:56

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

    0 讨论(0)
  • 2021-01-01 02:00

    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.

    0 讨论(0)
提交回复
热议问题