简体   繁体   English

如何仅使用内置模块使用Python 3上传大文件?

[英]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-TypeAccept不同值,但仍然无法正常工作。 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.

相关问题 使用python打开浏览器并单击,仅使用内置模块 - Use python to open browser and click, using only built-in modules 检查进程是否仅使用 Python 内置模块在 Windows 中运行 - Check if process is running in Windows using only Python built-in modules 如何不加区别地仅使用python内置功能(来自两个列表)来创建字典? - How can I create a dictionary without distinction and only using python built-in funtions(from two lists)? 使用内置的Python模块填写Web表单数据 - Filling Out Web Form Data Using Built-In Python Modules 如何使用Python IDLE查看内置函数的代码? - How do I view the code for built-in functions using the Python IDLE? 如何使用 exec() 在字符串的 function 中使用内置模块 - How to use built-in modules in a function of a string using exec() 如何在不使用内置函数的情况下减去python中具有相同长度的两个列表中的值(仅使用循环) - How can I subtract the values in two lists with the same lengths in python without using built-in function (only using loops) 如何获取python中的内置模块列表? - How to get a list of built-in modules in python? 如果只使用内置模板 django 系统,我是否需要操作 DRF? - Do i need to manipulate DRF if only im using the built-in template django system? 如何仅使用内置函数从 python 中的列表列表中生成格式良好的表 - How to produce a well-formatted table from a list of list in python only using built-in functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM