使用requests库进行文件上传的多种方法。
一、 仅上传文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import requests url = 'http://example.com/upload_file' file_path = '/path/to/file' with open (file_path, 'rb' ) as f: files = { 'file' : f} headers = { 'content-type' : 'multipart/form-data' } response = requests.request( 'POST' , url, files = files, headers = headers) print (response.text) #如果您需要上传多个文件,可以将它们保存在一个列表中,并将列表作为值传递给 files # 字典中的 'file' 键。例如: import requests url = 'http://example.com/upload_files' file_paths = [ '/path/to/file1' , '/path/to/file2' , '/path/to/file3' ] files = {} for i, file_path in enumerate (file_paths): with open (file_path, 'rb' ) as f: files[f 'file{i}' ] = f headers = { 'content-type' : 'multipart/form-data' } response = requests.request( 'POST' , url, files = files, headers = headers) print (response.text) |
二、 既要上传文件,又要上传参数
1) requets.request()方法中使用files参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import requests url = 'http://example.com/upload_file' file_path = '/path/to/file' data = { 'name' : 'John' , 'age' : 30 } with open (file_path, 'rb' ) as f: files = { 'file' : f} headers = { 'content-type' : 'multipart/form-data' } response = requests.request( 'POST' , url, files = files, data = data, headers = headers) print (response.text) #如果您需要上传多个文件,可以将它们保存在一个列表中,并将列表作为值传递给 files 字典中的 'file' 键。例如: import requests url = 'http://example.com/upload_files' file_paths = [ '/path/to/file1' , '/path/to/file2' , '/path/to/file3' ] data = { 'name' : 'John' , 'age' : 30 } files = {} for i, file_path in enumerate (file_paths): with open (file_path, 'rb' ) as f: files[f 'file{i}' ] = f headers = { 'content-type' : 'multipart/form-data' } response = requests.request( 'POST' , url, files = files, data = data, headers = headers) print (response.text) |
1) requets.request()方法中使用data参数, 不用files参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | """如果您想在使用 requests 库上传文件和传递参数时,不使用 `files` 参数而是使用`data` 参数,可以使用 `MultipartEncoder` 类。`MultipartEncoder` 类可以将文件和 参数打包成 `multipart/form-data` 格式的请求体,并将其传递给 requests 库的 `data` 参数。""" import requests from requests_toolbelt.multipart.encoder import MultipartEncoder url = 'http://example.com/upload_file' file_path = '/path/to/file' data = { 'name' : 'John' , 'age' : 30 } with open (file_path, 'rb' ) as f: encoder = MultipartEncoder(fields = { 'file' : ( 'filename' , f), * * data}) headers = { 'Content-Type' : encoder.content_type} response = requests.post(url, data = encoder, headers = headers) print (response.text) """ 在上面的代码中,我们指定了要上传的文件的路径(file_path)和上传的 URL(url),并指定了要传递的参数(data)。然后,我们使用 Python 的 with <br>语句打开文件,将其读取为二进制数据,并将其作为值传递给名为 `fields` 的字典中的 'file' 键。我们使用 `**data` 将参数字典中的所有键值对解包到 `fields` <br>字典中。接着,我们使用 `MultipartEncoder` 类将 `fields` 字典打包成 `multipart/form-data` 格式的请求体,并将其传递给请求的 `data` 参数。<br>最后,我们指定请求头中的 `Content-Type` 为 `encoder.content_type`,以确保请求体以 `multipart/form-data` 格式上传。我们使用 `requests.post` <br>方法将请求发送到指定的 URL。请注意,这种方法需要使用 `requests_toolbelt` 库中的 `MultipartEncoder` 类。如果您没有安装这个库,<br>可以通过 `pip install requests-toolbelt` 命令进行安装。另外,这种方法不支持上传多个文件,如果您需要上传多个文件,建议使用前面示例中的方法。""" |
3) 两者的区别
使用 `files` 参数的方法:
优点:
- 简单易用,可以方便地上传单个或多个文件。
- `requests` 库会自动将文件打包成 `multipart/form-data` 格式的请求体,无需手动设置请求头。
缺点:
- 由于 `requests` 库会自动将文件打包成 `multipart/form-data` 格式的请求体,因此可能会占用更多的内存。
- 对于大文件或多个文件的上传,可能会导致上传速度变慢或内存不足的情况。
不使用 `files` 参数的方法:
优点:
- 可以使用 `MultipartEncoder` 类将文件和参数打包成 `multipart/form-data` 格式的请求体,更加灵活。
- 可以手动设置请求头,更加精细地控制请求。
缺点:
- 相对来说比较复杂,需要使用 `MultipartEncoder` 类进行打包。
- 需要手动设置请求头,容易出错。
综上所述,使用 `files` 参数的方法相对来说更加简单易用,适用于上传单个或少量文件的情况;而不使用 `files` 参数的方法相对来说更加灵活,适用于上传大文件或多个文件的情况。具体使用哪种方法,可以根据实际情况进行选择。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通