uploadData.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import aiohttp
  2. import asyncio
  3. from config import Config
  4. async def upload(url, channel, classIndex, nvr, videoTime, filename, imgContent, rateLicensePlate, rateSpeed):
  5. payload = {
  6. 'channel': channel,
  7. 'classIndex': classIndex,
  8. 'ip': nvr,
  9. 'videoTime': videoTime,
  10. 'rateLicensePlate' : rateLicensePlate,
  11. 'rateSpeed' : rateSpeed
  12. }
  13. # 使用 aiohttp.FormData 构建文件上传的请求体
  14. data = aiohttp.FormData()
  15. for key, value in payload.items():
  16. data.add_field(key, value)
  17. # 文件上传,注意这里是 'file' 字段
  18. data.add_field('file', imgContent, filename=filename, content_type='image/jpeg')
  19. # 发送异步 POST 请求
  20. async with aiohttp.ClientSession() as session:
  21. async with session.post(url, data=data) as response:
  22. text = await response.text()
  23. return text
  24. if __name__ == "__main__":
  25. # for test
  26. conf = Config.get("test")
  27. url = conf["url"]
  28. channel = conf["channel"]
  29. classIndex = conf["classIndex"]
  30. nvr = conf["nvr"]
  31. # 测试配置
  32. # channel = "64"
  33. # classIndex = "68"
  34. # nvr = "172.19.152.231"
  35. # 生产配置
  36. channel = "251"
  37. classIndex = "101"
  38. nvr = "172.16.20.251"
  39. filename = "0-detectionPicture.jpg"
  40. with open(filename, "rb") as f:
  41. imgContent = f.read()
  42. videoTime = "2024-12-05 11:43:33"
  43. async def main():
  44. response = await upload(url, channel, classIndex, nvr, videoTime, filename, imgContent)
  45. print(await response.text()) # 获取响应文本内容
  46. asyncio.run(main())