如何向 aiohttp 请求 post 文件列表 python 请求模块?
How to aiohttp request post files list python requests module?
我想 post 使用 aiohttp。
而且,我需要 post 和 FILE。
但是,我的代码不起作用
这是我的代码
import aiohttp
file = open('file/path', 'rb')
async with aiohttp.request('post', url, files=file) as response:
return await response.text()
和request.FILES is None
这是引用
def post(self, url: StrOrURL,
*, data: Any=None, **kwargs: Any) -> '_RequestContextManager':
"""Perform HTTP POST request."""
return _RequestContextManager(
self._request(hdrs.METH_POST, url,
data=data,
> **kwargs))
E TypeError: _request() got an unexpected keyword argument 'files'
请....这个可能...?
我需要解决方案...拜托...T^T
这是期望的输出
request.FILES['key'] == file
密钥是 html 形式
<form method="post" name="file_form" id="file_form" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="key" id="file" />
<input type="submit" />
</form>
谢谢!它运作良好!
但我还有更多问题
我正在使用 from django.core.files.uploadedfile import InMemoryUploadedFile
这是我使用 py.test
的测试代码
def get_uploaded_file(file_path):
f = open(file_path, "rb")
file = DjangoFile(f)
uploaded_file = InMemoryUploadedFile(file, None, file_path, "text/plain", file.size, None, None)
return uploaded_file
file = get_uploaded_file(path)
async with aiohttp.request('post', url, data={'key': f}) as response:
return await response.text()
如何在测试中制作此代码...?
根据POST a Multipart-Encoded File - Client Quickstart - aiohttp documentation,您需要将文件指定为data
字典(值应该是类文件对象):
import asyncio
import aiohttp
async def main():
url = 'http://httpbin.org/anything'
with open('t.py', 'rb') as f:
async with aiohttp.ClientSession() as session:
async with session.post(url, data={'key': f}) as response:
return await response.text()
text = asyncio.run(main()) # Assuming you're using python 3.7+
print(text)
注意:字典键应该是 key
以匹配 <input type="file" name="key" id="file" />
中的 key
客户:
import requests
url = 'http://SERVER_ADDRESS:PORT/sendfile'
files = {'file': open('data.txt', 'r').read()}
requests.post(url, data=files)
服务器:
from aiohttp import web
PORT = 8082
async def send_file(request):
data = await request.post()
with open('data.txt', 'w') as f:
f.write(data['file'])
return web.Response(text='ok',content_type="text/html")
app = web.Application(client_max_size=1024**3)
app.router.add_post('/sendfile', send_file)
web.run_app(app,port=PORT)
要接收二进制数据,您可以使用请求的 read
函数。
例如,接收文本文件将如下所示:
from aiohttp import web
async def receive_post_request(request)
data = (await request.read()).decode('utf-8')
return web.Response(text='data', content_type="text/html")
我按照 documentation:
中所述使用 FormData 解决了问题
data = FormData()
data.add_field('file',open('input.tsv', 'rb'), filename='input.tsv', content_type='text/tab-separated-values')
async with session.post(url, data=data) as resp:
data = await resp.read()
我想 post 使用 aiohttp。 而且,我需要 post 和 FILE。 但是,我的代码不起作用 这是我的代码
import aiohttp
file = open('file/path', 'rb')
async with aiohttp.request('post', url, files=file) as response:
return await response.text()
和request.FILES is None
这是引用
def post(self, url: StrOrURL,
*, data: Any=None, **kwargs: Any) -> '_RequestContextManager':
"""Perform HTTP POST request."""
return _RequestContextManager(
self._request(hdrs.METH_POST, url,
data=data,
> **kwargs))
E TypeError: _request() got an unexpected keyword argument 'files'
请....这个可能...? 我需要解决方案...拜托...T^T
这是期望的输出
request.FILES['key'] == file
密钥是 html 形式
<form method="post" name="file_form" id="file_form" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="key" id="file" />
<input type="submit" />
</form>
谢谢!它运作良好!
但我还有更多问题
我正在使用 from django.core.files.uploadedfile import InMemoryUploadedFile
这是我使用 py.test
def get_uploaded_file(file_path):
f = open(file_path, "rb")
file = DjangoFile(f)
uploaded_file = InMemoryUploadedFile(file, None, file_path, "text/plain", file.size, None, None)
return uploaded_file
file = get_uploaded_file(path)
async with aiohttp.request('post', url, data={'key': f}) as response:
return await response.text()
如何在测试中制作此代码...?
根据POST a Multipart-Encoded File - Client Quickstart - aiohttp documentation,您需要将文件指定为data
字典(值应该是类文件对象):
import asyncio
import aiohttp
async def main():
url = 'http://httpbin.org/anything'
with open('t.py', 'rb') as f:
async with aiohttp.ClientSession() as session:
async with session.post(url, data={'key': f}) as response:
return await response.text()
text = asyncio.run(main()) # Assuming you're using python 3.7+
print(text)
注意:字典键应该是 key
以匹配 <input type="file" name="key" id="file" />
key
客户:
import requests
url = 'http://SERVER_ADDRESS:PORT/sendfile'
files = {'file': open('data.txt', 'r').read()}
requests.post(url, data=files)
服务器:
from aiohttp import web
PORT = 8082
async def send_file(request):
data = await request.post()
with open('data.txt', 'w') as f:
f.write(data['file'])
return web.Response(text='ok',content_type="text/html")
app = web.Application(client_max_size=1024**3)
app.router.add_post('/sendfile', send_file)
web.run_app(app,port=PORT)
要接收二进制数据,您可以使用请求的 read
函数。
例如,接收文本文件将如下所示:
from aiohttp import web
async def receive_post_request(request)
data = (await request.read()).decode('utf-8')
return web.Response(text='data', content_type="text/html")
我按照 documentation:
中所述使用 FormData 解决了问题data = FormData()
data.add_field('file',open('input.tsv', 'rb'), filename='input.tsv', content_type='text/tab-separated-values')
async with session.post(url, data=data) as resp:
data = await resp.read()