简体   繁体   English

如何从传入的POST中提取文件

[英]How do you extract a file from an incoming POST

I have a strange case where I have a POST coming in with a file attached. 我有一个奇怪的情况,我有一个附带文件的POST。 I am using aiohttp as a server. 我正在使用aiohttp作为服务器。 Normally I would use a get to retrieve a file but that's not possible in this situation. 通常,我会使用get来检索文件,但是在这种情况下是不可能的。 I can't seem to find any documentation on retrieving a file over http using aiohttp. 我似乎找不到任何有关使用aiohttp通过http检索文件的文档。

Simple example assuming you've used a form with enctype="multipart/form-data" : 假设您使用的表单带有enctype="multipart/form-data"简单示例:

async def handle_file_upload(request):
    data = await request.post()
    print('parts:', data.keys())
    # assuming we have a file called foobar
    print('filename:', data['foobar'].filename)
    print('file object:', data['foobar'].file)
    print('file content:', data['foobar'].file.read())
    ...  # (return response etc.)

be careful about using code like this in the wild with no protection as I believe await request.post() will read all request data into memory. 请谨慎使用await request.post()保护的此类代码,因为我相信await request.post()会将所有请求数据读入内存。 If someone does curl -X POST --data-binary "@/dev/urandom"... you'll be in big trouble. 如果有人curl -X POST --data-binary "@/dev/urandom"...您将遇到很大的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python从传入的HTTP POST中提取数据 - How to extract data from incoming HTTP POST using python 使用 Textract,如何通过 .py 脚本将 pdf 文件中的表格和 output 表格提取到 csv 文件中? - Using Textract, how do you extract tables from a pdf file and output it into a csv file via .py script? 如何从Google Reader导出的OPML文件中提取Feed网址? - How do you extract feed urls from an OPML file exported from Google Reader? 你如何使用python从json文件中提取一些数据 - how do you extract some data from json file using python 如何通过Client.post()从Django测试的表单中上传文件? - How do you upload a file from a form in a Django test through Client.post()? 你如何从for循环中提取元组的所有元素? - How do you extract all the elements of tuple from for loop? 如何从python列表中的元素中提取浮点数? - How do you extract the floats from the elements in a python list? 在python中,如何从字符串中提取某些字符? - In python how do you extract a certain characters from a string? "如何使用 python 从字符串中提取 url?" - How do you extract a url from a string using python? 如何使用ManyToManyField从对象的查询集中提取唯一的查询集 - How do you extract unique queryset from queryset of objects with ManyToManyField
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM