1

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.

1
  • You are much better off using mod_wsgi code.google.com/p/modwsgi mod_python was not under active development for a very long time and it looks like someone has just now picked it up again. As far as a framework for what you want to do Django is good or if you want something with a bit less complexity you could go with Flask flask.pocoo.org Commented Aug 14, 2013 at 17:49

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

is that the "action" itself? so if I did "action=php.execute('pythonscript.py')" in the <form> there, that would get the job done? I'm guessing I will still have to return the resultant page as a string.
take a look at this I'm not sure putting that code in action will work. I have little experience with php but from what I know about html I don't believe action=php.exec() will work. Also, the syntax would be exec("python yourPythonScript.py").
oh I thought that line that you have to put in htaaccess, uh..."Addhandler cgi-script .py .pl .cgi " took care of having to preface everything with perl or python or what have you.
As far as I know running a script with exec should have the same commands you would type into a terminal to run the python script. htaaccess might handle this but just to be sure I would keep the "python" command as part of the string you pass in to exec. I hope this works for you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.