【发布时间】:2021-09-25 02:36:24
【问题描述】:
我的一个要求是为整个文件夹中的每个视频发送一个上传请求。
如何改成多线程请求?
我的想法是导入线程,目的是上传到aws-s3。
import requests
import base64
import hashlib
import os
tools_dir = os.path.dirname(os.path.realpath(__file__))
project_root = os.path.dirname(tools_dir)
videos_dir = os.path.join(project_root, "output", "videos")
# Basic auth settings
basic_auth_user = "abc123"
basic_auth_password = "abc456"
auth = (basic_auth_user + ":" + basic_auth_password)
# Base64 encode and decode
auth_encode_format = auth.encode("utf-8")
auth_encode_base64 = base64.b64encode(auth_encode_format)
auth_encode_base64_format = auth_encode_base64.decode("utf-8")
str1 = "_"
for file in os.listdir(videos_dir):
# print(file[0:10:])
# print(file[:file.index(str1)])
serial_number = file[:file.index(str1)]
# print(f'{serial_number}')
m = hashlib.md5()
with open(os.path.join(videos_dir, file), 'rb') as f:
for chunk in iter(lambda: f.read(4096), b""):
m.update(chunk)
h = m.hexdigest()
url = "http://localhost:8000/api/awss3/upload"
payload = {
'fileHash': f'{h}',
'device': {'serialNumber':f'{serial_number}'}
}
files = [('file', (f'{file}', open(os.path.join(videos_dir, file), 'rb'), 'application/octet-stream'))]
headers = {
'Authorization': f'Basic {auth_encode_base64_format}'
}
response = requests.request("POST", url, headers = headers, data = payload, files = files)
print("\n" + response.text)
【问题讨论】: