python base64 /上传json格式/base64数据文件

梦里梦外; 2021-12-11 06:55 850阅读 0赞
  1. """ 上传json格式,base64数据文件。 """
  2. import time
  3. import json
  4. import base64
  5. import requests
  6. def get_base64str(file_path):
  7. """获取base64位字符串 bytes字节 -->base64字节 --> base64字符串 """
  8. str_base64 = None
  9. with open(file_path,'rb') as f:
  10. bytes_base64 = base64.b64encode(f.read())
  11. str_base64 = bytes_base64.decode('utf-8')
  12. assert str_base64 is not None
  13. return str_base64
  14. def save_file(str_base64,topath):
  15. """将base64还原存储为文件, 查看文件是否能正常打开, 以确认编码正确. """
  16. bytes_base64 = str_base64.encode('utf-8')
  17. with open(topath,'wb') as f:
  18. _bytes = base64.b64decode(bytes_base64)
  19. f.write(_bytes)
  20. if __name__ == '__main__':
  21. """请求调用API"""
  22. data = { "filetype":"base64","file":""}
  23. data["file"] = get_base64str("test.mp4")
  24. url = "http://192.168.56.102/api/verify"
  25. headers = { "Content-Type":"application/json"}
  26. res = requests.post(url=url,headers=headers
  27. data=json.dumps(data),timeout=(3.05,15))
  28. print(res.text)

发表评论

表情:
评论列表 (有 0 条评论,850人围观)

还没有评论,来说两句吧...

相关阅读