简体   繁体   English

如何将环境从Python Web应用程序传递到Perl程序?

[英]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.) (在后台,它会根据需要调用getenvputenv 。)

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.

相关问题 如何将环境变量传递到我的烧瓶应用程序 - How I can pass environment variables to my flask application 如何将数据从我的 PyQt5 UI 输入传递到我的 python 程序? - How can I pass data from my PyQt5 UI input to my python program? 我如何阻止我的python程序崩溃 - How can i stop my python program from crashing 如何从我的 python 程序中调用智能合约? - how can I call smart contract from my python program? 如何将结果从 python 装饰器传递给我的类? - How can I pass the result from a python decorator to my class? 如何从Web界面获取查询,在python代码中使用查询,然后在perl代码中读取结果? - How can I get a query from web interface, use it in a python code and then read the result in a perl code? 如何防止 pyzmq 阻止我的 python 应用程序? - How can I prevent pyzmq from blocking my python application? 如何在桌面上像普通程序一样运行Python程序? - How can I run my Python program like a normal program from the desktop? 如何从Python程序创建的文件中读取Java程序中的RDD - How can I read RDD in my java program from a file that was created by Python program 我可以将TensorFlow Python环境从Windows转移到Ubuntu吗? - Can I transfer my TensorFlow Python environment from Windows to Ubuntu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM