如何将现有脚本集成到Django中?

2024-10-02 04:35:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚介绍了Django框架,我想将这个框架用于我所在小组的许多基于系统的Perl/Python/wlst脚本。我如何获得正常的命令行输出,并让它以近乎实时的方式输出浏览器?我研究过连载,但似乎找不到好的教程。我只是在寻找关于如何制作一个普通的Python/Perl脚本并使其与基于Django的web站点一起工作的建议。在

具体来说:我有一个脚本,它将用户添加到一个组中,添加到一个weblogic域的角色中。我想让你填写表单,它连接到服务器上做工作并返回输出。唯一的问题是它需要一点时间来连接。在


Tags: django命令行用户脚本框架web站点系统
2条回答

我想你得更具体一点。 如果要在浏览器中输出“操作”的结果,则必须使用呈现模板的view。在

引用django的docs on views

A view function, or view for short, is simply a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image . . . or anything, really. The view itself contains whatever arbitrary logic is necessary to return that response. This code can live anywhere you want, as long as it’s on your Python path.

因此,如果您有一个正在工作的python函数/脚本,您可以将其导入视图,并将结果通过管道传输到模板中。在

您想在web浏览器中siplay外部脚本结果吗?然后,我建议您让这些脚本将结果写入数据库,然后使用django模型获取和显示信息。在

如果要在每次用户打开网页并显示结果时执行脚本,请使用以下python代码:

def command_output(cmd):
    """ Capture a command's standard output."""
    import subprocess;
    return subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()[0];

result = command_output('ls -al');

相关问题 更多 >

    热门问题