aiohttp相关 - 上传文件

  • 作者:KK

  • 发表日期:2020.02.12


很简单,就是在发请求的时候加个files参数即可

import aiohttp, asyncio, urllib

async def main():
	async with aiohttp.ClientSession() as session:
		async with session.post(
			'http://xxx.com/upload',
			files = { #声明这个参数即可
				'upload_image': urllib.request.open('http://www.kkh86.com/mylife/data/img/article/cover/1467123274.jpg'), #网络文件
				#'upload_image': open('/data/www/project1/111.jpg', 'rb'), #本地文件
			}
		) as response:
			print(await response.text())

loop = asyncio.get_event_loop()
loop.run_until_complete(main())