Running Inline::Python in a CGI script

I'm trying to use Inline::Python within a CGI script. The script is
run under the latest version of Webmin's miniserv.pl web server.

I have written two very variations of a very simple script.

1. The first version embeds a couple of Python functions in the DATA
portion of the Perl cgi. This version executes fine both from the cli
and as a cgi.

2.The second version imports a class from a Python module. The script
runs fine when called from the cli. However, it borks when executed as
a cgi, complaining of calls to undefined functions/methods. Of course,
those functions/methods are in the Python module.

The only thing I can think might be an issue is the difference in
environment between the shell and the web server. However, a dump of
both environments shows no significant difference, so I'm stumped.

Perl: 5.14.2
Python: 2.7.3
Inline: 0.51
Inline::Python: 0.41
OS: Ubuntu 12.10
Webmin: 1.620

Kind Regards,
Chris
0
cnighswonger
3/11/2013 3:20:36 PM
perl.inline 1285 articles. 0 followers. Follow

9 Replies
636 Views

Similar Articles

[PageSpeed] 31


-----Original Message----- 
From: Chris Nighswonger
Sent: Tuesday, March 12, 2013 2:20 AM
To: inline@perl.org
Subject: Running Inline::Python in a CGI script

> I'm trying to use Inline::Python within a CGI script.

Hi Chris,

Is there anything of relevance in the "Inline CGI" example in 
C/C-Cookbook.pod ?

(FAIK, that might be completely irrelevant where Inline::Python is concerned 
..... but it does seem feasible to me that the problem could occur because, 
in the CGI environment, a needed "read" permission is not provided.)

Cheers,
Rob 

0
sisyphus1
3/11/2013 9:21:29 PM
--e89a8fb1fbba09453c04d7ba3b42
Content-Type: text/plain; charset=UTF-8

Hi Rob,

On Mon, Mar 11, 2013 at 5:21 PM, <sisyphus1@optusnet.com.au> wrote:

 I'm trying to use Inline::Python within a CGI script.
>>
>
>
> Is there anything of relevance in the "Inline CGI" example in
> C/C-Cookbook.pod ?
>
> (FAIK, that might be completely irrelevant where Inline::Python is
> concerned .... but it does seem feasible to me that the problem could occur
> because, in the CGI environment, a needed "read" permission is not
> provided.)
>

I double checked and .Inline and friends have correct perms. Clearing
everything below .Inline shows that the module is rebuilt as expected on
the next call to the CGI.

I've tried BUILD_NOISY and the other Inline switches which would seem to be
helpful for debug, but with little success at obtaining useful information.
I'm guessing some of that may be largely dependent upon how the author of
Inline::Python has implemented them.

It seems there should be some way to convince inline::Python to fess up to
any errors it is encountering along the way.

Thanks for your help.

Kind Regards,
Chris

--e89a8fb1fbba09453c04d7ba3b42--
0
cnighswonger
3/12/2013 1:25:40 PM
--e89a8ff242ab03b07504d7ba60f2
Content-Type: text/plain; charset=UTF-8

On Tue, Mar 12, 2013 at 9:25 AM, Chris Nighswonger <
cnighswonger@foundations.edu> wrote:

> I double checked and .Inline and friends have correct perms. Clearing
> everything below .Inline shows that the module is rebuilt as expected on
> the next call to the CGI.
>
> I've tried BUILD_NOISY and the other Inline switches which would seem to
> be helpful for debug, but with little success at obtaining useful
> information. I'm guessing some of that may be largely dependent upon how
> the author of Inline::Python has implemented them.
>
> It seems there should be some way to convince inline::Python to fess up to
> any errors it is encountering along the way.
>

Perhaps I should mention this as well:

The CGI scripts executed by miniserv.pl via an eval of a code block which
sets up the environment and calls up the actual CGI via 'do.' See
https://github.com/webmin/webmin/blob/master/miniserv.pl#L2337 for the
exact code.

I suspected the forced package change on line 2339 might be causing some
scoping problems or some such, so I did this:

root@biblios:/usr/share/webmin/pykota (0.02_pre_alpha *)# perl -e 'package
pyktoa; do "PyKotaConfig.cgi"; die $@ if ($@);'

And get this:

Can't locate object method "new" via package "PykotaConfig" (perhaps you
forgot to load "PykotaConfig"?) at PyKotaConfig.cgi line 33.

This is exactly the same error I receive when running the same script as
CGI. BUT... commenting out line 2339 in miniserve.pl did not fix the
problem. So I suspect there is more to it than that.

Kind Regards,
Chris

--e89a8ff242ab03b07504d7ba60f2--
0
cnighswonger
3/12/2013 1:36:01 PM
--nextPart2818458.cuCtDNOQf3
Content-Transfer-Encoding: 7Bit
Content-Type: text/plain; charset="us-ascii"

On Monday 11 March 2013 11:20:36 Chris Nighswonger wrote:

> 2.The second version imports a class from a Python module. The script
> runs fine when called from the cli. However, it borks when executed as
> a cgi, complaining of calls to undefined functions/methods. Of course,
> those functions/methods are in the Python module.

Can you post the script code? Or at least the part dealing with Python?
When importing Inline::Python it loads and runs the given Python code 
(containing the import statements). Then it inspects the main namespace and 
imports those names into the Perl namespace. I suspect that miniserv.pl 
trickery leads to namespace problems. So could you please try the 
py_new_object() and py_call_function() way of accessing Python? This avoids 
namespace magic and should be more reliable:

# create a Python foo.Bar object and make it look like a Bar object in Perl:
my $bar = py_new_object('Bar', 'foo', 'Bar', 'constructor arg 1');
$bar->baz();

Regards,
Stefan
--nextPart2818458.cuCtDNOQf3
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)

iEYEABECAAYFAlE/MRMACgkQ1QuEJQQMVriooQCfViNhdVL43WeY+SjrfpoN/jvT
ikwAn12ov4TAyTyZ/2lBhOnUWS4Mcx1p
=a78A
-----END PGP SIGNATURE-----

--nextPart2818458.cuCtDNOQf3--

0
nine
3/12/2013 1:43:45 PM
--14dae93b5740d94dce04d7baa18e
Content-Type: text/plain; charset=UTF-8

Forgot to 'reply all'... sorry.

---------- Forwarded message ----------
From: Chris Nighswonger <cnighswonger@foundations.edu>
Date: Tue, Mar 12, 2013 at 9:53 AM
Subject: Re: Running Inline::Python in a CGI script
To: Stefan Seifert <nine@detonation.org>


On Tue, Mar 12, 2013 at 9:43 AM, Stefan Seifert <nine@detonation.org> wrote:

> On Monday 11 March 2013 11:20:36 Chris Nighswonger wrote:
>
> > 2.The second version imports a class from a Python module. The script
> > runs fine when called from the cli. However, it borks when executed as
> > a cgi, complaining of calls to undefined functions/methods. Of course,
> > those functions/methods are in the Python module.
>
> Can you post the script code? Or at least the part dealing with Python?
>

See the Perl script below. The Python module may be seen here:
http://svn.pykota.com/pykota/trunk/pykota/config.py



> When importing Inline::Python it loads and runs the given Python code
> (containing the import statements). Then it inspects the main namespace and
> imports those names into the Perl namespace. I suspect that miniserv.pl
> trickery leads to namespace problems. So could you please try the
> py_new_object() and py_call_function() way of accessing Python? This avoids
> namespace magic and should be more reliable:
>
> # create a Python foo.Bar object and make it look like a Bar object in
> Perl:
> my $bar = py_new_object('Bar', 'foo', 'Bar', 'constructor arg 1');
> $bar->baz();
>

I'll try this and post back the results.

Kind Regards,
Chris

----------

#!/usr/bin/perl

use strict;
use warnings;

BEGIN {
    push(@INC, "..");
};


use Inline  Python      => 'DATA',
            DIRECTORY   => '/usr/share/webmin/pykota/.Inline/';

Inline->init();

my $pykota_conf = new PykotaConfig('/etc/pykota/');
print "Admin mail for the AP400N: " . $pykota_conf->getAdminMail('AP400N')
.. "\n";

1;

__DATA__
__Python__

from pykota.config import PyKotaConfig as PykotaConfig

--14dae93b5740d94dce04d7baa18e--
0
cnighswonger
3/12/2013 1:54:25 PM
--e89a8f6436fc5c9b6c04d7bbc4c4
Content-Type: text/plain; charset=UTF-8

On Tue, Mar 12, 2013 at 9:43 AM, Stefan Seifert <nine@detonation.org> wrote:

> # create a Python foo.Bar object and make it look like a Bar object in
> Perl:
> my $bar = py_new_object('Bar', 'foo', 'Bar', 'constructor arg 1');
> $bar->baz();
>

So here's my shot at this:

use Inline::Python qw( py_new_object );

my $pykota_conf = py_new_object("config", "pykota.config", "PyKotaConfig",
'/etc/pykota/');
print "Admin mail for the AP400N: " . $pykota_conf->getAdminMail('AP400N')
.. "\n";

And here's what it says when I execute it at the cli:

root@biblios:/usr/share/webmin/pykota (0.02_pre_alpha *)# ./PyKotaConfig.cgi
'PyKotaConfig' is not a callable object at
/usr/local/lib/perl/5.14.2/Inline/Python.pm line 317.

Keep in mind all of my experience lies with Perl, and I have basically zero
experience with Python, so I may be misunderstanding how Python
packages/classes are referenced.

When I do py_study_package, PyKotaConfig clearly shows up as a class,
though.

Kind Regards,
Christopher

--e89a8f6436fc5c9b6c04d7bbc4c4--
0
cnighswonger
3/12/2013 3:15:40 PM
--f46d0444edf1f23afc04d7e13743
Content-Type: text/plain; charset=UTF-8

On Tue, Mar 12, 2013 at 11:15 AM, Chris Nighswonger <
cnighswonger@foundations.edu> wrote:

> use Inline::Python qw( py_new_object );
>
> my $pykota_conf = py_new_object("config", "pykota.config", "PyKotaConfig",
> '/etc/pykota/');
>
> print "Admin mail for the AP400N: " . $pykota_conf->getAdminMail('AP400N')
> . "\n";
>
> And here's what it says when I execute it at the cli:
>
> root@biblios:/usr/share/webmin/pykota (0.02_pre_alpha *)#
> ./PyKotaConfig.cgi
> 'PyKotaConfig' is not a callable object at
> /usr/local/lib/perl/5.14.2/Inline/Python.pm line 317.
>
>
This call appears to work fine when executed in Python:

root@biblios:/usr/share/webmin/pykota (0.02_pre_alpha *)# python -c 'import
pykota.config; config = pykota.config.PyKotaConfig("/etc/pykota"); print
config.getAdminMail("AP400N");'
foobar@foundations.edu
root@biblios:/usr/share/webmin/pykota (0.02_pre_alpha *)#

So is it a syntax problem in my py_new_object call or is it a bug in
Inline::Python?

Kind Regards,
Chris

--f46d0444edf1f23afc04d7e13743--
0
cnighswonger
3/14/2013 11:56:33 AM
--nextPart2410736.zYgzx4rAqX
Content-Transfer-Encoding: 7Bit
Content-Type: text/plain; charset="us-ascii"

On Tuesday 12 March 2013 11:15:40 Chris Nighswonger wrote:
> On Tue, Mar 12, 2013 at 9:43 AM, Stefan Seifert <nine@detonation.org> wrote:
> > # create a Python foo.Bar object and make it look like a Bar object in
> > Perl:
> > my $bar = py_new_object('Bar', 'foo', 'Bar', 'constructor arg 1');
> > $bar->baz();
> 
> So here's my shot at this:
> 
> use Inline::Python qw( py_new_object );
> 
> my $pykota_conf = py_new_object("config", "pykota.config", "PyKotaConfig",
> '/etc/pykota/');
> print "Admin mail for the AP400N: " . $pykota_conf->getAdminMail('AP400N')
> . "\n";

You're missing the import statement. It's still neccessary for Python to load 
the package. Try:

use Inline::Python qw( py_new_object );
use Inline::Python => <<'PYTHON';
    import pykota.config
PYTHON

my $pykota_conf = py_new_object("config", "pykota.config", "PyKotaConfig",
'/etc/pykota/');
print "Admin mail for the AP400N: " . $pykota_conf->getAdminMail('AP400N')
.. "\n";


Regards,
Stefan
--nextPart2410736.zYgzx4rAqX
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)

iEYEABECAAYFAlFBvNYACgkQ1QuEJQQMVrhzcQCdEU3Udo9y9XD3kQIiY9TCKSgD
y0cAmwfozj93VpYATObEJ5RJa714gplP
=JK4D
-----END PGP SIGNATURE-----

--nextPart2410736.zYgzx4rAqX--

0
nine
3/14/2013 12:04:36 PM
--e89a8fb1f8d0832ae604d7e37f7a
Content-Type: text/plain; charset=UTF-8

On Thu, Mar 14, 2013 at 8:04 AM, Stefan Seifert <nine@detonation.org> wrote:

> On Tuesday 12 March 2013 11:15:40 Chris Nighswonger wrote:
> > So here's my shot at this:
> >
> > use Inline::Python qw( py_new_object );
> >
> > my $pykota_conf = py_new_object("config", "pykota.config",
> "PyKotaConfig",
> > '/etc/pykota/');
> > print "Admin mail for the AP400N: " .
> $pykota_conf->getAdminMail('AP400N')
> > . "\n";
>
> You're missing the import statement. It's still necessary for Python to
> load
> the package.
>

That was most of it. Here is what finally worked:

use Inline::Python qw( py_new_object py_call_method );
use Inline  Python   => 'DATA',
            DIRECTORY   => '/usr/share/webmin/pykota/.Inline/';

Inline->init();

my $pykota_conf = py_new_object("PyKotaConfig", "pykota.config",
"PyKotaConfig", '/etc/pykota/');

print "Admin mail for the AP400N: " . py_call_method($pykota_conf,
"getAdminMail", 'AP400N') . "\n";

I ended up having to use py_call_method rather than Perl's method syntax.
Maybe I just missed something else about that along the way.

Many thanks to Stefan and Rob for the help and bearing with me.

And thanks for a very cool module which will hopefully save me loads of
execution time over making system calls.

Kind Regards,
Chris

--e89a8fb1f8d0832ae604d7e37f7a--
0
cnighswonger
3/14/2013 2:39:43 PM
Reply:

Similar Artilces:

Doing Inline-less Inline
A stated objective of Inline 0.50 is to allow modules using Inline to work even if the user doesn't have Inline. Here is one implementation idea - the distribution contains pre-computed Inline files, and creates a dual-use (with or without Inline) object file installation, which is accessed by a very small and simple Inline::Everywhere reflector. A module wishing this functionality might say use Inline::Everywhere ...; instead of use Inline ...; Or perhaps use Inline::Everywere ..., BUT_DONT_USE_INLINE_BY_DEFAULT => 1; if one wished to insulate the module from th...

Need some help with Script and Inline Text running through Script
I need some help with this. Basically I want ONLY the last word of my text to run through the Script that makes it glow. The problem I am having at the moment is that when I use DIV tags to isolate the last word in the displayed text it drops it down (carriage return). I do not want this. I have tried SPAN tages but the Script will not work with those. I borrowed the "glow" Script from online, and have modified it for my use. Perhaps someone could help me so that the text sentence is ALL in line with the last word going through the glow/flicker Script?  Also, this Script work...

Problems with using Inline in cgi script
--_346d51ca-f725-46d7-a2c4-e284d7376a0e_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi all, I am trying to use C code in a cgi script and have a couple= of problems.1. When I execute my code from the web, I get the following er= ror,Couldn't find an appropriate DIRECTORY for Inline to use.I am thinking = this probably has something to do with permissions??? Where is a good place= for Inline to create a directory while running within a CGIscript? (The cg= i-bin doesn't really seem like a good choice and my s...

Inline script in ASCX Control
Hello, I have an UpdatePanel on my page that contains a ContentPlaceholder. A series of navigation links on my form allows the user to cycle through a series of ASCX controls which are rendered to the ContentPlaceholder. Some of these controls contain inline script blocks which should be executed when the ASCX form loads. I have noticed that after a callback the scripts are not executed ater the UpdatePanel refreshes. Could anyone tell me how to get these scripts to run? Ideally I'd like to keep the script in the markup of the ASCX rather than generating them dynamically and Registerin...

On Inline::Python
Greetings, I've been using Inline::Python (0.40) quite successfully in Ubuntu, but I have not been able to compile it on Windows with Cygwin. Is there any way to get this to compile on Windows? Any advice would be appreciated. Respectfully yours, Ryan P.S. This is great software you've made. I hope you continue developing it, because I'll definitely continue using it. Thanks for the hard work. :D ----- Original Message ----- From: "Ry" <ryanblonna3@gmail.com> To: <inline@perl.org> Sent: Monday, December 05, 2011 2:33 AM Subject: On ...

Using Inline Python to import Python vars
--e89a8f923cd2c5352d04d94f84d3 Content-Type: text/plain; charset=UTF-8 I seem to be misunderstanding something about importing Python package vars with Inline. I'm thinking it may be some syntax problem. This code returns the value of __version__ as expected: use Inline Python => "from pykota.version import __version__; print __version__;"; However, this code borks: use Inline Python => 'DATA', DIRECTORY => '/usr/share/webmin/pykota/.Inline/', NAME => 'PyKota::Test'; print pykota...

Inline::Python Python child process stdout redirection
--e89a8fb1f8d05d836f04d83cc67b Content-Type: text/plain; charset=UTF-8 Hi all, I'm using Inline::Python to call a Python function which writes to stdout. I'm guessing that Inline::Python forks at some point to run Python. Is there a way to redirect the stdout of the resulting child process? I'm (again) guessing that there is no way of knowing the process id in time to do anything with it. Kind Regards, Chris --e89a8fb1f8d05d836f04d83cc67b-- On Monday 18 March 2013 21:10:24 Chris Nighswonger wrote: > I'm using Inline::Python to call a Python function wh...

Should inline.h be renamed inline.c ?
porting/args_assert.t only looks for .c files. If a function is placed into inline.h which has such assertions, args_assert.t won't find them, and fails. We could add a special case into args_assert.t for inline.h, or we could rename inline.h to be inline.c. The other header files that contain inline functions have a .c suffix already, such as dquote_static.c Karl Williamson wrote: > porting/args_assert.t only looks for .c files. If a function is placed > into inline.h which has such assertions, args_assert.t won't find them, > and fails. > > We...

cgi script that takes in data then runs script on different host
Hi All, I've got a cgi form that takes in data. Then, I want the data to be passed to a script that's waiting on a DIFFERENT host. So, say I have this pseudo-code: cgi script on host 1 pass $name to host 2 script on host 2 # get $name ---------------------------> print "hi there $name!\n"; Obviously, I'll be doing something a little more in-depth than printing the variables, but you get the idea! I went through perlfaq 8 in the perldocs, but I didn't understand the networking jargon :-( Thanks! Simon ===== #####...

running cgi script
It's been a long time since I tried to work with cgi scripting, & I never did learn a whole lot about cgi. I'm trying to run a simple script straight out of the Perl Black Book to display the CGI environment hash to the web browser screen (I'll want to do other things with it later, but thought I'd start with this). This is on a Sun Solaris, running Netscape webserver if that matters. Here's the script: #!/usr/local/bin/perl use CGI; $co = new CGI; print $co->header, $co->start_html('Test Environment'), $co->ce...

Inline script issue
I have a user control in my ViewPage that I am trying to assign some values to:<uc:UserControl StringProperty="<%= ViewData.Value %>" />When I step in to the UC to see what StringProperty is getting set to, its value is "<%= ViewData.Value %>". I guess the UC is getting built up before the inline code is run, so my question is - what should I do differently? Thanks/Rex  try setting the value of the usercontrol in codebehindVikram www.vikramlakhotia.comPlease mark the answer if it helped you Thanks for the suggestion! However, since using inline ...

How to run cgi script
------_=_NextPart_001_01C90C44.AEA1359F Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi, =20 I have installed IIS on my machine (to make my machine web server). Now i need to execute / run cgi script on my machine. cgi script is saved in c:\irfan. =20 now how should i run this cgi script in web browser. which webURL i should give. =20 Regards Irfan. =20 ------_=_NextPart_001_01C90C44.AEA1359F-- Irfan J Sayed (isayed) wrote: > Hi, > > I have installed IIS on my machine (to make my machine web server). Now...

Inline::C and Inline::C::Struct question
Hi, I have installed the Inline::C (0.43) and Inline::Struct (0.06) and I still can't run the example code that contains a struct. Is there something I am missing, like a ENV variable not set or something foolish like that? I pretty much gave up on using structs directly from Perl, but I can't even define/create an instance of one from whithin the inline C call. It however works if I have an include file. So here is the code that works: PL -- #!/usr/bin/perl use Inline C => << 'ENDING'; #include<other.c> void geta(int aa) { in...

cgi scripts are not running
i have installed bugzilla, but my cgi scripts are not running properly, whether its problem in apache or execcgi, plz post ur valuable suggestions On Mon, Dec 12, 2005 at 03:51:45AM -0800, sid wrote: > i have installed bugzilla, but my cgi scripts are not running properly, > whether its problem in apache or execcgi, plz post ur valuable > suggestions Follow the installation instructions. Read the errorlog of apache. Tell us what you did. -- Regards, Olav Without more information, it's difficult to help. What does checksetup.pl tell you? What does testserver...

Web resources about - Running Inline::Python in a CGI script - perl.inline

Resources last updated: 12/27/2015 4:47:30 AM