如何从tornado http文件中读取文件内容?

时间:2014-05-07 12:48:51

标签: python http tornado

我的文件是这样的,但我无法正确执行内容。我整个下午都在这上面,但仍然很困惑。主要原因是我不知道[file_obj [0] ['body']]是什么样的。

这是我的代码的一部分

# user_file  content 
"uid = 'h123456789'"
"data = [something]"
# end of user_file

# code piece

file_obj = req.request.files.get('user_file', None)  

for i in file_obj[0]['body']:
    i.strip('\n')  # I tried comment out this line, still can't work
    exec(i) 

# I failed

你能告诉我file_obj体中user_file conentent会是什么样子吗?这样我就可以找出解决方案。我用http表单提交给龙卷风。

非常感谢。

1 个答案:

答案 0 :(得分:0)

也许这会有所帮助。

#first file object in request.
file1 = self.request.files['file1'][0]

#where the file content actually placed.
content = file1['body']

#split content into lines, unix line terminals assumed.
lines = content.split(b'\n')

for l in lines:
    #after decoding into strings, you're free to execute them.
    try:
        exec(l.decode())
    except:
        pass