0
我正在尝试使用Openload REST API调用upload a file“240p.mp4”。如何使用Openload.co API上传文件
由于上传端点都需要文件的SHA-1散列,我得到了它这样做:
sha1 = hashlib.sha1()
BLOCKSIZE = 65536
with open('240p.mp4', 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
sha1.update(buf)
buf = afile.read(BLOCKSIZE)
sha1_hash = sha1.hexdigest()
所以我要求的上传链接:
url = "https://api.openload.co/1/file/ul?login={login}&key={key}&sha1={sha1}".format(
login='YOUR_LOGIN',
key='YOUR_API_KEY',
sha1=sha1_hash,
)
p = {
'url': url,
'headers': {
'User-Agent': self.ua,
}
}
r = self.r.get(url=p['url'], headers=p['headers'])
j = r.json()
upload_link = j['result']['url']
他们建议作出CURL但我更喜欢requests
guy ^^
curl -F [email protected]/path/to/file.txt https://13abc37.example.com/ul/jAZUhVzeU78
所以,我试图用POST请求来复制它:
p = {
'url': upload_link,
'headers': {
'user-agent': self.ua,
'Content-Type': 'multipart/form-data; boundary="xxx"',
},
'files': {
'file1': open('/scripts/wordpress/240p.mp4', "rb"),
# I've also tried this (and some others)
# 'file1': ('240.mp4', open('/scripts/wordpress/240p.mp4', "rb"), 'video/mp4')
}
}
r = self.r.post(url=p['url'], headers=p['headers'], files=p['files'])
但它返回这个错误响应:
r.content¬
{
"status": 500,
"msg": "failed to read: closed"
}
从理论上讲,状态500错误是从服务器错误。但为什么我得到这个错误?
N1:我明确地设置了boundary="xxx"
,因为如果我不这样做。该响应返回它缺失。所以我设定了它。
N2:文件/scripts/wordpress/240p.mp4
的路径是正确的。权限也是如此。
N3:我知道远程上传功能,但我需要从二进制文件上传(从本地我的意思)
N4:很明显的,但self.r
是requests.session()
的分配
使用https://github.com/mohan3d/PyOpenload – Dalvenjia
我要去尝试。谢谢。如果它有效,我会检查代码以知道我的代码在哪里失败... – Jeflopo