获取文件而不保存 aiohttp discord.py



所以我有一个命令,它将用户在命令后所说的内容发送到网站 api,并发送网站生成的文件。但是,我正在切换到 aiohttp,因为它不会像标准请求函数那样阻塞

这就是我对正常请求进行操作的方式,它工作正常:

elif (data[0].lower() == ">signgirl"):
await bot.send_typing(message.channel)
tmp = message.content.replace(">signgirl", "")
m = hashlib.md5()
m.update(tmp.encode('utf-8'))
print(tmp, m.hexdigest())
r = requests.post("http://localhost/sign.php", stream=True, data={'text': tmp})
if (r.status_code() == 200):
await bot.send_file(destination=message.channel, filename=str(m.hexdigest()+".png"), fp=r.raw)

但是,当我尝试使用aiohttp时,我不知道如何实际获取原始文件数据。 所以我做了这个函数来得到它。但是它不允许我返回图像,并且我无法在不导致错误的情况下检查HTTP状态代码。

async def post_data2(url, payload):
async with aiohttp.ClientSession() as session2:
async with session2.post(url, data=payload) as response2:
output = {}
output['data'] = await Image.open(BytesIO(response2.read()))
output['status'] = 200 #await str(response2.status()) #Why is this object not callable?
return output

我还能怎么做?这可能吗?AIOHTTP似乎并不容易理解。

先生日"V" 来自 discord.py discord服务器发送了一个获取和发送数据的完美示例

async with aiohttp.ClientSession() as session:
# or use a session you already have
async with session.get("http://example.com") as resp:
buffer = io.BytesIO(await resp.read())
# buffer is a file-like
await client.send_file(channel, fp=buffer, filename="whatever")

最新更新