如何从 Python 调用 Perl 脚本,通过管道输入?

发布于 2024-07-19 02:06:16 字数 448 浏览 5 评论 0原文

我将对 DomainKeys 和 DKIM 的一些支持融入到一个开源电子邮件营销程序中,该程序使用 python 脚本通过 SMTP 发送实际的电子邮件。 我决定走快速而肮脏的路线,只编写一个 Perl 脚本,该脚本接受来自 STDIN 的电子邮件,对其进行签名,然后将其返回签名。

我想要做的是,从 python 脚本,将字符串中的电子邮件文本通过管道传输到 perl 脚本,并将结果存储在另一个变量中,这样我就可以发送签名的电子邮件。 然而,我并不是一个Python专家,而且我似乎找不到一个好的方法来做到这一点。 我很确定我可以使用诸如 os.system 之类的东西来实现此目的,但是将变量通过管道传输到 perl 脚本似乎让我感到困惑。

简而言之:如何将变量从 python 脚本传输到 perl 脚本,并将结果存储在 Python 中?

编辑:我忘记包括我正在使用的系统只有 python v2.3

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

宛菡 2024-07-26 02:06:16

使用子进程。 这是 Python 脚本:

#!/usr/bin/python

import subprocess

var = "world"

pipe = subprocess.Popen(["./x.pl", var], stdout=subprocess.PIPE)

result = pipe.stdout.read()

print result

这是 Perl 脚本:

#!/usr/bin/perl

use strict;
use warnings;

my $name = shift;

print "Hello $name!\n";

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";
甜味拾荒者 2024-07-26 02:06:16

os.popen() 将返回一个带有标准输入和标准输出的元组的子流程。

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

遇到 2024-07-26 02:06:16
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()
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()
心如狂蝶 2024-07-26 02:06:16

“我很确定我可以使用 os.system 之类的东西来实现这一点,但将变量管道传输到 perl 脚本似乎让我感到困惑。”

正确的。 subprocess 模块类似于 os.system,但提供了您正在寻找的管道功能为了。

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

他不在意 2024-07-26 02:06:16

我确信您选择您选择的路线是有原因的,但为什么不直接用 Python 进行签名呢?

你签的怎么样? 也许我们可以在编写 python 实现方面提供一些帮助?

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?

维持三分热 2024-07-26 02:06:16

我也尝试这样做,仅配置如何使其工作,因为

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

我希望这将帮助您使其工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文