如何从 Python 调用 Perl 脚本,将输入传递给它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/798413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a Perl script from Python, piping input to it?
提问by Alex Fort
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.
我正在将一些对 DomainKeys 和 DKIM 的支持添加到一个开源电子邮件营销程序中,该程序使用 python 脚本通过 SMTP 发送实际电子邮件。我决定走快速而肮脏的路线,只编写一个 perl 脚本,它接受来自 STDIN 的电子邮件,对其进行签名,然后将其返回。
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.
我想要做的是从 python 脚本中,将字符串中的电子邮件文本通过管道传输到 perl 脚本,并将结果存储在另一个变量中,这样我就可以发送签名的电子邮件。然而,我并不完全是一个 Python 大师,而且我似乎找不到一个好的方法来做到这一点。我很确定我可以os.system
为此使用类似的东西,但是将变量通过管道传输到 perl 脚本似乎是我无法做到的。
In short: How can I pipe a variable from a python script, to a perl script, and store the result in Python?
简而言之:如何将变量从 python 脚本传递到 perl 脚本,并将结果存储在 Python 中?
EDIT: I forgot to include that the system I'm working with only has python v2.3
编辑:我忘了包括我正在使用的系统只有 python v2.3
回答by Chas. Owens
Use subprocess. Here is the Python script:
使用子流程。这是 Python 脚本:
#!/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:
这是 Perl 脚本:
#!/usr/bin/perl
use strict;
use warnings;
my $name = shift;
print "Hello $name!\n";
回答by millimoose
os.popen()will return a tuple with the stdin and stdout of the subprocess.
os.popen()将返回一个包含子进程的标准输入和标准输出的元组。
回答by ngn
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()
回答by Jon Cage
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?
我敢肯定,您选择的路线是有原因的,但为什么不直接在 Python 中进行签名呢?
How are you signing it? Maybe we could provide some assitance in writing a python implementation?
你怎么签的?也许我们可以在编写 python 实现时提供一些帮助?
回答by S.Lott
"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."
“我很确定我可以为此使用 os.system 之类的东西,但是将变量传递给 perl 脚本似乎是我无法理解的事情。”
Correct. The subprocessmodule is like os.system, but provides the piping features you're looking for.
正确的。该子模块就像使用os.system,但提供的管道功能,你要寻找的。
回答by Fatih Karatana
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.
我希望这会帮助你让它工作。