Question

So I recently discovered mod_python, which is great because its not hard to rewrite the PHP code I had and execute through Apache. However, I just found out that my hosting service, (HostGator), does not support this module because they have a single apache user instance per physical shared server.

This begs the question:

Without the use of something like Django, Pylons, etc, is there a way to invoke python routines server side from HTML code and have it return web content?

Example:

Simple form

<form name="message" action="directory/pythonscript.py/function" method="POST">

where, upon submission, the form parses the content in the form, and then the python routine returns a string of the newly assembled HTML file based on the input of the form, and the browser presumably re-renders it.

def function(args):
     #parse inputs
     #assemble html string
     return htmlString

Additional question:

With mod_python, you must pass at least the "req" arg, which points to the apache request structure. Is this functionality still present without mod_python? Would I be able to somehow physically map to that without mod_python?

I'm presuming that all of this is possible because HostGator does support python and a lot of the modules, smtpd for example, have functionality to send emails, communicate over the web, etc, so presumably there is some way to access this API independent of Apache???

99% of searches for this return info about Django, Pylons, etc. If that is indeed the best way to do it, I will explore that option, but all I want to do for now is create some dynamic HTML content with a back end python handler without having to install any other APIs.

Thanks.

EDIT

I should clarify that I am not attached to mod_python at all, it is just something I found. My question is geared towards avoiding non-standard, (standard = shipped with Python 2.7.x, useable through apache or otherwise), APIs if possible.

Was it helpful?

Solution

After some digging, it seems that the quickest way to do this is through php, but not as per the previous answer.

In my HTML I should have:

<html> <form name="form" method="$POST" action="formAction.php" </html>

in formAction.php:

<?

system( 'python ~/scripts/pythonscript.py/function', $return)
echo $return

?>

where $return is the dynamically generated HTML page string. Within the python script, I'll have to parse the posted values from the HTML session somehow. I think that answers my question but I'll wait and see if someone else posts something more interesting.

EDIT: Figured out how to parse the POSTed values:

<?php

$VAR1 = $_GET["var1"];
$VAR2 = $_GET["var2"];

system("python <script>.py -args=[".$VAR1.",".$VAR2"."]", $return)
echo $return

?>

for example.

OTHER TIPS

You could do a php.execute('pythonScript.py') on submit, right?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top