简体   繁体   English

在python上通过路径上传视频文件不起作用

[英]upload video file by path on python not working

I tried to upload video file using python, the problem is the system cannot find the file even though i write the path of file. 我试图使用python上传视频文件,问题是系统找不到文件,即使我写了文件的路径。 my code is like this: 我的代码是这样的:

import os
import requests

#step 1
host = 'https://blablabla.com'

test = {
    "upload_phase" : "start",
    "file_size" : 1063565
}

params = {
    "access_token":my_access_token, 
    "fields":"video_id, start_offset, end_offset, upload_session_id", 
}

vids = requests.post(host, params=params, data=test)
vids = vids.json()

try:
    video_id= vids["video_id"],
    start_offset= vids["start_offset"],
    end_offset= vids["end_offset"],
    upload_session_id= vids["upload_session_id"]
except:
    pass

print(vids)

###############################################################################
#step 2
###############################################################################
test = {
    "upload_phase" : "transfer",
    "start_offset" : start_offset,
    "upload_session_id": upload_session_id,
    "video_file_chunk": os.path.realpath('/home/def/Videos/test.mp4')
}

params = {
    "access_token":my_access_token, 
    "fields":"start_offset, end_offset", 
}

vids = requests.post(host, params=params, data=test)
vids = vids.json()

try:
    start_offset= vids["start_offset"],
    end_offset= vids["end_offset"]
except:
    pass

print(vids)

Many way i tried, like os.path.abspath, os.path, os.path.dirname, os.path.basename, os.path.isfile, os.path.isabs, os.path.isdir it's still doesn't work. 我试过很多方式, like os.path.abspath, os.path, os.path.dirname, os.path.basename, os.path.isfile, os.path.isabs, os.path.isdir它仍然没有工作。 even i give import os.path or import os . 即使我提供import os.path or import os

In your code you just send path to your file as the string to server, but not file itself. 在您的代码中,您只需将文件路径作为字符串发送到服务器,但不是文件本身。 You should try something like: 你应该尝试类似的东西:

my_file = {'file_to_upload': open(os.path.realpath('/home/def/Videos/test.mp4'),'rb')} 
# You should replace 'file_to_upload' with the name server actually expect to receive
# If you don't know what server expect to get, check browser's devconsole while uploading file manually
vids = requests.post(host, params=params, files=my_file)

Also note that you might need to use requests.Session() to be able to handle cookies, access token... 另请注意,您可能需要使用requests.Session()才能处理cookie,访问令牌......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM