[英]How can I pass the environment from my Python web application to a Perl program?
How do I set Perl's %ENV to introduce a Perl script into the context of my web application? 如何设置Perl的%ENV将Perl脚本引入Web应用程序的上下文中?
I have a website, written in a language different from Perl (Python). 我有一个网站,用不同于Perl(Python)的语言编写。 However I need to use a Perl application, which consists of a .pl file: 但是,我需要使用Perl应用程序,该应用程序由.pl文件组成:
#!/usr/bin/env perl
"$ENV{DOCUMENT_ROOT}/foo/bar.pm" =~ /^(.+)$/;
require $1;
my $BAR = new BAR(
user => 'foo',
);
print $bar->get_content;
... and a module bar.pm, which relies on "$ENV{HTTP_HOST}"
, "$ENV{REQUEST_URI}"
, "$ENV{REMOTE_ADDR}"
and "$ENV{DOCUMENT_ROOT}"
. ...以及一个模块bar.pm,它依赖于"$ENV{HTTP_HOST}"
, "$ENV{REQUEST_URI}"
, "$ENV{REMOTE_ADDR}"
和"$ENV{DOCUMENT_ROOT}"
。
How should I set this hash? 我应该如何设置此哈希? This is my very first experience with Perl, so I may be missing something really obvious here :) 这是我第一次接触Perl,因此在这里我可能会遗漏一些非常明显的东西:)
If you're spawning that Perl process from your Python code (as opposed to "directly from the webserver"), there are several ways to set the child process environment from the Python parent process environment, depending on what you're using for the "spawning". 如果您是从Python代码生成Perl进程(而不是“直接从Web服务器”生成),则有多种方法可从Python父进程环境设置子进程环境,具体取决于您使用的是“产卵”。
For example, if you're using subprocess.Popen
, you can pass an env=
argument set to the dictionary you desire, as the docs explain: 例如,如果您使用subprocess.Popen
,则可以将env=
参数集传递给所需的字典,如文档所述 :
If env is not None, it must be a mapping that defines the environment variables for the new process; 如果env不为None,则它必须是为新进程定义环境变量的映射。 these are used instead of inheriting the current process' environment, which is the default behavior. 使用它们代替继承当前流程的环境,这是默认行为。
Perl's special %ENV
hash is the interface to the environment. Perl的特殊%ENV
哈希是与环境的接口。 (Under the hood, it calls getenv
and putenv
as appropriate.) (在后台,它会根据需要调用getenv
和putenv
。)
For example: 例如:
$ cat run.sh
#! /bin/bash
export REMOTE_ADDR=127.0.0.1
perl -le 'print $ENV{REMOTE_ADDR}'
$ ./run.sh
127.0.0.1
Your web server ought to be setting these environment variables. 您的Web服务器应该设置这些环境变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.