cgi.FieldStorage如何存储文件? [英] How does cgi.FieldStorage store files?

查看:287
本文介绍了cgi.FieldStorage如何存储文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我一直在研究原始WSGI,cgi.FieldStorage和文件上传。而且我不明白它如何处理文件上传。



起初,它似乎只是将整个文件存储在内存中。而且我认为hm,应该很容易测试-大文件会堵塞内存!..事实并非如此。不过,当我请求文件时,它是一个字符串,而不是迭代器,文件对象或其他任何东西。



我尝试读取cgi模块的源代码,发现了一些有关临时文件,但它返回一个怪异的字符串,而不是文件(类似于文件)对象!那么... fscking是如何工作的呢?!



这是我使用的代码:

 从wsgiref.simple_server导入cgi 
导入make_server

def应用程序(environ,start_response):
start_response('200 OK',[(' Content-Type','text / html')])
output =
< form action = method = post enctype = multipart / form-data>
< input type = file name = failas />
< input type = submit value = Varom />
< / form>

fs = cgi.FieldStorage(fp = environ ['wsgi.input'],environ = environ)
f = fs.getfirst('failas')
打印类型( f)
返回输出


如果__name__ =='__main__':
httpd = make_server('',8000,app)
print'Serving '
httpd.serve_forever()

预先感谢! :)

解决方案

检查 cgi模块描述,其中有一段讨论了如何处理文件上传。


如果字段代表上传的文件,则通过value属性或 getvalue()方法访问该值会读取内存中的整个文件字符串。这可能不是您想要的。您可以通过测试filename属性或 file 属性来测试上传的文件。然后,您可以从file属性中随意读取数据:




  fileitem = form [ userfile] 
如果fileitem.file:
#这是一个上传的文件;计算行
的行数= 0
而1:
的行= fileitem.file.readline()
如果不是行的话:中断
行数=行数+ 1

关于您的示例, getfirst()只是一个版本 getvalue()的值。
尝试替换

  f = fs.getfirst('failas')

  f = fs ['failas '] .file 

这将返回一个类似文件的对象,可读性很强。 / p>

So I've been playing around with raw WSGI, cgi.FieldStorage and file uploads. And I just can't understand how it deals with file uploads.

At first it seemed that it just stores the whole file in memory. And I thought hm, that should be easy to test - a big file should clog up the memory!.. And it didn't. Still, when I request the file, it's a string, not an iterator, file object or anything.

I've tried reading the cgi module's source and found some things about temporary files, but it returns a freaking string, not a file(-like) object! So... how does it fscking work?!

Here's the code I've used:

import cgi
from wsgiref.simple_server import make_server

def app(environ,start_response):
    start_response('200 OK',[('Content-Type','text/html')])
    output = """
    <form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="failas" />
    <input type="submit" value="Varom" />
    </form>
    """
    fs = cgi.FieldStorage(fp=environ['wsgi.input'],environ=environ)
    f = fs.getfirst('failas')
    print type(f)
    return output


if __name__ == '__main__' :
    httpd = make_server('',8000,app)
    print 'Serving'
    httpd.serve_forever()

Thanks in advance! :)

解决方案

Inspecting the cgi module description, there is a paragraph discussing how to handle file uploads.

If a field represents an uploaded file, accessing the value via the value attribute or the getvalue() method reads the entire file in memory as a string. This may not be what you want. You can test for an uploaded file by testing either the filename attribute or the file attribute. You can then read the data at leisure from the file attribute:

fileitem = form["userfile"]
if fileitem.file:
    # It's an uploaded file; count lines
    linecount = 0
    while 1:
        line = fileitem.file.readline()
        if not line: break
        linecount = linecount + 1

Regarding your example, getfirst() is just a version of getvalue(). try replacing

f = fs.getfirst('failas')

with

f = fs['failas'].file

This will return a file-like object that is readable "at leisure".

这篇关于cgi.FieldStorage如何存储文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆