FAQ
I am running a Python application under apache web server, executing as a
cgi script. Most of the output is print statements that write HTML.

I'd like to embed some PHP code within the HTML. The PHP is a gallery
plugin script that talks to the core photo gallery application, written in
php.

The idea is that my python app can display a random image from a photo
album, using the photo gallery application's plugin script.

If I do: 'print "<?php ... ?>"', it doesn't work. I imagine because the
web server never sees it as php code.

Is there a way to do this?

Some possible ideas:
Is there a way I can get python to call functions within a PHP app?
Perhaps write the plugin in a separate script in PHP, and include that
page within my output so that apache recognises the page inclusion? How
to get apache to pay attention to the output so it "wakes up" and does
some server-side includes?

Many thanks for any help.
--
NickC

Search Discussions

  • Duncan Booth at Jan 24, 2010 at 10:37 am

    NickC wrote:

    Some possible ideas:
    Is there a way I can get python to call functions within a PHP app?
    Perhaps write the plugin in a separate script in PHP, and include that
    page within my output so that apache recognises the page inclusion? How
    to get apache to pay attention to the output so it "wakes up" and does
    some server-side includes?
    One option would be to ditch either the Python or the PHP and do everything
    in one language.

    Probably your simplest option is to get the web server to generate the PHP
    output separately. Since it's the same web server you can just use some
    javascript in the final web page to call in the PHP generated content with
    ajax. Alternatively, use urllib in Python to retrieve a page from the
    Apache server and insert that into its own output: that way you won't
    requrie javascript in the client, but that might be messy if you have
    authentication or sessions going on.
  • NickC at Jan 24, 2010 at 1:59 pm

    On Sun, 24 Jan 2010 10:37:51 +0000, Duncan Booth wrote:

    content with ajax. Alternatively, use urllib in Python to retrieve a
    page from the Apache server and insert that into its own output: that
    Thanks for hint on urllib. I shake my head in amazement with python
    sometimes. I'll write it here:

    print urllib.urlopen('http://myserver.com/phpscript.php').read()

    That's it. *One* line.

    The output from the php script is:
    <a href='zenphoto/kitty/IMG_0759.jpg' title='Random Picture...'>
    <img src='zenphoto/cache/kitty/IMG_0759_800.jpg'
    alt="random image"
    IMG_0759" /></a>

    and the one-liner seamlessly prints that to insert it into the html output.

    And I thought it would be hard; I should have known better.


    --
    NickC
  • Steve Holden at Jan 24, 2010 at 11:07 pm

    NickC wrote:
    On Sun, 24 Jan 2010 10:37:51 +0000, Duncan Booth wrote:

    content with ajax. Alternatively, use urllib in Python to retrieve a
    page from the Apache server and insert that into its own output: that
    Thanks for hint on urllib. I shake my head in amazement with python
    sometimes. I'll write it here:

    print urllib.urlopen('http://myserver.com/phpscript.php').read()

    That's it. *One* line.

    The output from the php script is:
    <a href='zenphoto/kitty/IMG_0759.jpg' title='Random Picture...'>
    <img src='zenphoto/cache/kitty/IMG_0759_800.jpg'
    alt="random image"
    IMG_0759" /></a>

    and the one-liner seamlessly prints that to insert it into the html output.

    And I thought it would be hard; I should have known better.
    Always great to see Python doing impressive things for new users.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
    Holden Web LLC http://www.holdenweb.com/
    UPCOMING EVENTS: http://holdenweb.eventbrite.com/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJan 24, '10 at 8:09a
activeJan 24, '10 at 11:07p
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase