简体   繁体   English

在网络上执行Python程序

[英]Executing Python program on web

Can I use os.system() or subprocess.call() to execute a Python program on a webserver? 我可以使用os.system()subprocess.call()对网络服务器执行的Python程序?

I mean can I write these functions in a .py script and run it from a web browser and expect the program to be executed? 我的意思是,我可以在.py脚本中编写这些功能,然后从网络浏览器中运行它并期望该程序被执行吗?

Thanks a lot. 非常感谢。

EDIT: Sorry for all the confusion, I am giving you more background to my problem. 编辑:对不起,所有的混乱,我给你更多的背景我的问题。

The reason I am trying to do is this. 我要这样做的原因是这样的。 I have a Python program that accepts an XML file and returns me TTF file. 我有一个Python程序,该程序接受XML文件并向我返回TTF文件。 I run that program in terminal like this: 我在终端中像这样运行该程序:

ttx somefile.xml 

After which it does all the work and generates a ttf file. 之后,它将完成所有工作并生成一个ttf文件。 Now when I deploy this script as a module on web server. 现在,当我将此脚本部署为Web服务器上的模块时。 I use a to allow user to browse and select the XML file. 我使用来允许用户浏览和选择XML文件。 Then I read the file data to temporary file and then pass the file to the module script to be executed like this: 然后,我将文件数据读取到临时文件,然后将该文件传递给要执行的模块脚本,如下所示:

ttx.main([temp_filename]) 

Is this right way to do it? 这是正确的方法吗? Because at this point, I don't get any error in the log or in browser. 因为在这一点上,我在日志或浏览器中都没有收到任何错误。 I get blank screen. 我出现黑屏。

When this didn't work, I was going to try os.system or subprocess.call 当这个没有工作,我会尝试os.systemsubprocess.call

You do not use os.system or subprocess.call to execute something as a cgi process. 您不使用os.system或subprocess.call将某些内容作为cgi进程执行。

Maybe you should read the Python cgi tutorial here: 也许您应该在这里阅读Python cgi教程:

http://www.cs.virginia.edu/~lab2q/ http://www.cs.virginia.edu/~lab2q/

If you want your cgi process to communicate with another process on your local machine, you might want to look at "REST frameworks" for Python. 如果您希望cgi进程与本地计算机上的另一个进程进行通信,则可能需要查看Python的“ REST框架”。

Yes, I do it all the time. 是的,我一直都这样做。 Import as you would do normally, stick your .py in your cgi-bin folder and make sure the server is capable of handling python. 像平常一样导入,将.py粘贴到cgi-bin文件夹中,并确保服务器能够处理python。

Another option would be to simply create an application on Google's App Engine. 另一种选择是仅在Google的App Engine上创建一个应用程序。 That gives you oodles of resources and APIs for Python execution. 这为您提供了大量执行Python的资源和API。

http://code.google.com/appengine http://code.google.com/appengine

So long as your server is configured to run CGI scripts (Apache's documentation for that is here , for example), yes, you can execute a python script from a webserver. 只要将服务器配置为运行CGI脚本(例如,有关Apache的文档在此处 ),是的,您可以从Web服务器执行python脚本。 Simply make sure the script is in the appropriate cgi-bin/ directory and that the file has executable permission on the server. 只需确保脚本在适当的cgi-bin /目录中,并且该文件具有服务器上的可执行权限。

With regards to your comments: 关于您的意见:

  1. You can, if you really want, explicitly allow other folders on the server to run executable code. 如果确实需要,可以明确允许服务器上的其他文件夹运行可执行代码。 I don't know what server you're using, but on Apache this is done by setting Option +ExecCGI for the folder you want. 我不知道您使用的服务器是什么,但是在Apache上,这是通过为所需的文件夹设置Option +ExecCGI来完成的。 Again, see the docs I linked to. 同样,请参阅我链接到的文档。
  2. You need to give an absolute path with respect to the server. 您需要提供相对于服务器的绝对路径。 As an example, a site I develop has the layout: /public_html/cgi-bin/ When I want to access .cgi or .py files, the url for the site is something like http://chess.narnia.homeunix.com/cgi-bin/index.cgi . 例如,我开发的网站的布局为:/ public_html / cgi-bin /当我要访问.cgi或.py文件时,该网站的网址类似于http://chess.narnia.homeunix.com /cgi-bin/index.cgi You can also set up re-directs to certain files if you want. 您还可以根据需要设置对某些文件的重定向。
  3. One way to pass parameters through your browser is to append them to the URL like an HTTP POST method. 通过浏览器传递参数的一种方法是像HTTP POST方法一样将其附加到URL。 Here's a good example of doing that. 这是一个很好的例子。

Is that what you were looking for with your question, or did you want to actually invoke the python script with os.system() ? 这是您在寻找问题时想要的东西,还是您想使用os.system()实际调用 python脚本?

I've done it quite a bit in classic ASP on IIS 5 and above. 在IIS 5及更高版本的经典ASP中,我已经做了很多工作。 I would have the ASP engine execute python code (instead of, eg, vbscript (hearkening back to the old days, here)). 我会让ASP引擎执行python代码(而不是例如vbscript(在这里回溯到过去))。 Behind those asp pages would be python modules written in straight python that could be imported and could execute pretty much arbitrary code. 在这些asp页面的后面是用直接python编写的python模块,可以将其导入并执行几乎任意代码。 As others have mentioned, the effective user needs to have execute permission on the thing you're trying to execute. 正如其他人提到的那样,有效用户需要对您要执行的事情具有执行权限。

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

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