在 Python 中使用子进程
我正在创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
subprocess.Popen
有一个非常容易理解的语法:只需浏览一下看看这些例子,你就会得到模式。
subprocess.Popen
has a pretty understandable syntax:Just look through the examples and you'll get the pattern.
您只需传递二进制名称(在
$PATH
或相对位置)及其参数的元组或列表即可。You simply pass a tuple or list of your binary name (in
$PATH
or relative) and its arguments.request.FILES['file']
不是字符串文件名;它是一个UploadedFile
对象。请参阅 Django 文档 以了解可以对该对象执行哪些操作。尝试获取路径名并执行副本是不好的形式,因为这会对远程用户造成破坏。在这种用法中,Web 浏览器将文件数据上传到服务器,服务器将其传递给 Django,Django 创建 UploadedFile 来处理它。要简单地将该文件复制到磁盘,您需要如下代码:
如果您不想使用上传的名称,请使用除
f.name
之外的其他名称。如果您想对上传的文件运行命令,请首先将其保存在某个位置(可能是临时文件中),然后使用 subprocess.Popen 运行该命令。request.FILES['file']
is not a string filename; it's anUploadedFile
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:
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 usesubprocess.Popen
to run the command.