简体   繁体   English

在通用Web平台上从HTML执行Python

[英]Executing Python From HTML on Generic Web Platform

So I recently discovered mod_python, which is great because its not hard to rewrite the PHP code I had and execute through Apache. 所以我最近发现了mod_python,这很棒,因为它不难重写我拥有的PHP代码并通过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 . 但是,我发现我的托管服务(HostGator)不支持此模块,因为它们每个物理共享服务器只有一个apache用户实例

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? 如果不使用Django,Pylons等工具,是否可以通过HTML代码从服务器端调用python例程,并使其返回Web内容?

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. 在提交时,表单会解析表单中的内容,然后python例程会根据表单的输入返回新组装的HTML文件的字符串,然后浏览器大概会重新呈现它。

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. 使用mod_python,您必须至少传递“ req” arg,它指向apache请求结构。 Is this functionality still present without mod_python? 如果没有mod_python,此功能是否仍然存在? Would I be able to somehow physically map to that without mod_python? 没有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??? 我想所有这些都是可能的,因为HostGator确实支持python,并且许多模块(例如smtpd)具有发送电子邮件,通过网络进行通信等功能,因此大概有某种方法可以独立访问此API的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. 99%的搜索都返回有关Django,Pylons等的信息。如果这确实是最好的方法,我将探索该选项,但是我现在要做的就是创建一些带有后端的动态HTML内容。 python处理程序,而无需安装任何其他API。

Thanks. 谢谢。

EDIT 编辑

I should clarify that I am not attached to mod_python at all, it is just something I found. 我应该澄清一下,我根本没有附加到mod_python,这只是我发现的东西。 My question is geared towards avoiding non-standard, (standard = shipped with Python 2.7.x, useable through apache or otherwise), APIs if possible. 我的问题是为了避免使用非标准的API(标准= Python 2.7.x附带,可通过apache或其他方式使用),API。

After some digging, it seems that the quickest way to do this is through php, but not as per the previous answer. 经过一番挖掘之后,似乎最快的方法是通过php,但并非按照先前的答案。

In my HTML I should have: 在我的HTML中,我应该具有:

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

in formAction.php: 在formAction.php中:

<?

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

?>

where $return is the dynamically generated HTML page string. 其中$ return是动态生成的HTML页面字符串。 Within the python script, I'll have to parse the posted values from the HTML session somehow. 在python脚本中,我将不得不以某种方式解析HTML会话中的发布值。 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: 编辑:弄清楚如何解析POSTed值:

<?php

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

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

?>

for example. 例如。

您可以在提交时执行php.execute('pythonScript.py'),对吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM