在 Python 中使用子进程

发布于 2024-11-01 07:10:26 字数 832 浏览 1 评论 0原文

我正在创建一个 Django 应用程序,它从用户那里获取输入文件。我想使用 subprocess 模块获取文件并将其作为参数传递给外部脚本并取回结果。 subprocess.Popen 调用的格式是什么。我还想将一个选项传递给脚本,例如 -a。 换句话说,对于看起来像这样的命令行, subprocess.Popen 调用看起来像这样:

./myscript -option file

另外,关于我尝试运行的脚本的路径是否存在任何问题。多谢。

这是我在views.py 中使用的代码。我只是想看看一个简单的 cp 命令是否有效以及如何传递参数:

def upload_file(request):

    if request.method == 'POST':
    form=UploadFileForm()        
    form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['file'])
            return HttpResponseRedirect('/upload')
    else:
        form = UploadFileForm()
    return render_to_response('upload_file.html', {'form': form})

def handle_uploaded_file(f):

    p=subprocess.Popen(['/bin/cp',f , '/home/dutzy/Desktop'])

I am creating a Django application that takes an input file from a user. I want to use the subprocess module to take the file and pass it as an argument to an external script and take back the results. What would be the format for the subprocess.Popen call. I would like, to also pass an option to the script like -a.
In other words how would a subprocess.Popen call look like for a command line that looks something like this:

./myscript -option file

Also are there any issues regarding the path of the script i am trying to run. Thanks a lot.

This is the code that I am using in my views.py. I am just trying to see if a simple cp command works and how to pass the arguments:

def upload_file(request):

    if request.method == 'POST':
    form=UploadFileForm()        
    form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['file'])
            return HttpResponseRedirect('/upload')
    else:
        form = UploadFileForm()
    return render_to_response('upload_file.html', {'form': form})

def handle_uploaded_file(f):

    p=subprocess.Popen(['/bin/cp',f , '/home/dutzy/Desktop'])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野の 2024-11-08 07:10:26

subprocess.Popen 有一个非常容易理解的语法:

subprocess.Popen(['./myscript', '-option', 'file'])

只需浏览一下看看这些例子,你就会得到模式。

subprocess.Popen has a pretty understandable syntax:

subprocess.Popen(['./myscript', '-option', 'file'])

Just look through the examples and you'll get the pattern.

故事还在继续 2024-11-08 07:10:26

您只需传递二进制名称(在 $PATH 或相对位置)及其参数的元组或列表即可。

import subprocess
p = subprocess.Popen(('./myscript', '-option', 'file'))

You simply pass a tuple or list of your binary name (in $PATH or relative) and its arguments.

import subprocess
p = subprocess.Popen(('./myscript', '-option', 'file'))
甜扑 2024-11-08 07:10:26

request.FILES['file'] 不是字符串文件名;它是一个 UploadedFile 对象。请参阅 Django 文档 以了解可以对该对象执行哪些操作。

尝试获取路径名并执行副本是不好的形式,因为这会对远程用户造成破坏。在这种用法中,Web 浏览器将文件数据上传到服务器,服务器将其传递给 Django,Django 创建 UploadedFile 来处理它。要简单地将该文件复制到磁盘,您需要如下代码:

def handle_uploaded_file(f):
    destination = open('path/to/store/at/' + f.name, 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()

如果您不想使用上传的名称,请使用除 f.name 之外的其他名称。如果您想对上传的文件运行命令,请首先将其保存在某个位置(可能是临时文件中),然后使用 subprocess.Popen 运行该命令。

request.FILES['file'] is not a string filename; it's an UploadedFile object.Refer to the Django docs to see what you can do with that object.

It is bad form to try to get the pathname and execute a copy since that will break for a remote user. In this usage, the web browser is uploading the file data to your server, which passes it to Django, which creates the UploadedFile to handle it. To simply copy that file to disk, you need code like:

def handle_uploaded_file(f):
    destination = open('path/to/store/at/' + f.name, 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()

If you don't want to use the uploaded name, use something else besides f.name. If you want to run a command on the uploaded file, first save it somewhere (maybe in a temporary file) and then use subprocess.Popen to run the command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文