django - 访问临时上传文件的另一个应用程序

时间:2017-10-05 11:06:47

标签: python django logparser

我的目标:我正在尝试上传事件日志文件(.evtx),使用LogParser工具将其在后端转换为csv,然后可视化数据。 Logparser工具将使用os.system(cmdline_string)通过命令行运行。基本上我试图解析上传的文件而不将临时上传的文件保存到文件系统。

我的问题:我正在使用Log Parser的命令行解析事件日志。我无法使用日志解析器的命令行访问appdata \ local \ temp中的临时上载文件。

代码

uploadfile.html

<form method="post" enctype="multipart/form-data">
  {% csrf_token %}
  <input type="file" name="myfile">
  <br>
  <button type="submit">Upload</button>
</form>

views.py

import os
import pandas as pd

def uploadfile():
    if request.method == 'POST' and 'myfile' in request.FILES:
        myfile = request.FILES['myfile']

    filename, fileext = os.path.splitext(myfile.name)

    if fileext == '.evtx':
        cmdstring = "logparser " + \
                    "\"SELECT * INTO " + "output.csv" + \
                    " FROM " + myfile.temporary_file_path() + "\" " + \
                    "-i:EVT -o:CSV"
        os.system(cmdstring)
        # This will output error: 
        # Cannot open <from-entity>: Error opening event log /path/to/uploadedfiles: 
        # The process cannot access the file because it is being used by another process

我猜可能django正在访问临时文件,因此logparser无法访问该文件。当我尝试将文件保存到文件系统时,logparser能够访问和解析文件。当我只是访问并解析AppData \ Local \ Temp目录中的任何事件日志数据时,它也会设法执行此操作。

如何让另一个应用程序访问django中的临时上传文件?

我的环境:Windows 10,Python 3.6.1。 Django 1.11.4,Log Parser 2.2

0 个答案:

没有答案
相关问题