[英]How do I upload a big file using Python 3 using only built-in modules?
I'm trying to upload a file 32+ MB to a server using an API. 我正在尝试使用API将32+ MB的文件上传到服务器。 The only restriction I have is that can only use built-in modules. 我唯一的限制是只能使用内置模块。 I have seen many examples using requests
library, but I'm trying to solve with urllib
. 我已经看到了许多使用requests
库的示例,但是我正在尝试使用urllib
解决。 Using curl
as PoC, I did the job like this: 使用curl
作为PoC,我做了这样的工作:
curl -v --request POST --url 'https://domain/upload/long-string/' --form 'apikey=my-api-key' --form 'file=@my-file.extension'
Using urllib
, I wrote the code below, but it doesn't work, because the server always returns a 400
error: 使用urllib
,我编写了以下代码,但由于服务器始终返回400
错误,因此无法正常工作:
import urllib
def post_bigfile(upload_url, file, auth, timeout):
headers = {'Accept': '*/*', 'Content-Type': 'multipart/form-data'}
data = {'file': file, 'apikey': auth}
req = urllib.request.Request(upload_url, headers=headers,
data=urlencode(data).encode('utf-8'), method='POST')
return urllib.request.urlopen(req, timeout=timeout)
post_bigfile('https://domain/upload/long-string/', open('my-file.extension','rb'), 'my-api-key', 20)
I have tried using different values of Content-Type
and Accept
, but it still doesn't work. 我尝试使用Content-Type
和Accept
不同值,但仍然无法正常工作。 What could I possibly doing wrong? 我可能做错了什么? Is there another built-in module I could use to better solve this problem? 是否可以使用另一个内置模块更好地解决此问题?
A hint to what you are doing wrong can be found here: 您可以在这里找到关于您做错了什么的提示:
'Content-Type': 'multipart/form-data'
headers=headers
I was also busy with curl commands a while ago which were pushing multipart form-data and investigating doing it with built-in python libraries only (ie not requests). 不久前,我还忙于curl命令,该命令用于推送多部分表单数据,并研究仅使用内置的python库(即不请求)。 Once you put --form
into curl
you don't even need to give curl the --headers "Content-Type: multipart/form-data"
argument, it just defaults to it. 一旦将--form
放入curl
您甚至不需要为curl加上--headers "Content-Type: multipart/form-data"
参数,它只是默认设置。 Maybe you didn't realise curl was doing that. 也许您没有意识到curl正在这样做。
Have a look at this , python does not support that Mime type. 看看这个 ,Python不支持MIME类型。 A Python issue is linked https://bugs.python.org/issue3244 and I think your best bet is this script: https://pymotw.com/3/urllib.request/#uploading-files if you really want to do that with standard libraries only. 一个Python问题链接到https://bugs.python.org/issue3244 ,我认为最好的选择是以下脚本: https : //pymotw.com/3/urllib.request/#uploading-files(如果您确实想这样做)仅使用标准库。
Am assuming you don't control the server which is receiving that file and it looks like it wants multipart/form-data with the two elements "apikey" and "file". 假设您不控制正在接收该文件的服务器,并且看起来它想要带有两个元素“ apikey”和“ file”的multipart / form-data。 Unless you can alter what that server wants to something which urllib can POST you will have to use requests or see if you can get that big MultiPartForm
class to work. 除非您可以将服务器所需的内容更改为urllib可以发布的内容,否则您将不得不使用请求或查看是否可以使大型MultiPartForm
类正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.