Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
等效python请求调用CURL请求以上载图像_Python_Curl_Python Requests - Fatal编程技术网

等效python请求调用CURL请求以上载图像

等效python请求调用CURL请求以上载图像,python,curl,python-requests,Python,Curl,Python Requests,我使用的是Python请求模块,但无论我尝试上载什么图像,它都会成功,但图像在打开/读取时会出错。 我将图像编码为base64,设置内容类型标题(图像/png、图像/jpeg等)等 无论如何,我使用CURL执行以下操作,它是有效的: curl -u test@test.ca:test -H 'Content-Type: image/jpeg' --data-binary @test.jpeg -X POST 'https://test.test.com/api/upload.json?filen

我使用的是Python请求模块,但无论我尝试上载什么图像,它都会成功,但图像在打开/读取时会出错。 我将图像编码为base64,设置内容类型标题(图像/png、图像/jpeg等)等

无论如何,我使用CURL执行以下操作,它是有效的:

curl -u test@test.ca:test -H 'Content-Type: image/jpeg' --data-binary @test.jpeg -X POST 'https://test.test.com/api/upload.json?filename=test.jpeg'

这个请求与python中的请求模块(头文件等)的等效性是什么?

要复制
curl
命令,您不需要在base64中编码图像:
--data binary@test.jpeg
curl选项按原样发送
test.jpeg
文件:

import requests

r = requests.post('https://example.com/api/upload.json?filename=test.jpeg', 
                  data=open('test.jpeg', 'rb'), 
                  headers={'Content-Type': 'image/jpeg'},
                  auth=('test@test.ca', 'test')) # username, password

要复制
curl
命令,不需要在base64中对图像进行编码:
--data binary@test.jpeg
curl选项按原样发送
test.jpeg
文件:

import requests

r = requests.post('https://example.com/api/upload.json?filename=test.jpeg', 
                  data=open('test.jpeg', 'rb'), 
                  headers={'Content-Type': 'image/jpeg'},
                  auth=('test@test.ca', 'test')) # username, password

files={'test.png':base64.b64encode(open('test.png','r').read())}我已经在做那种类型的请求了。谢谢你的回复。我需要把它包括在文件里。它可以工作,但是上传后无法打开/读取图像。但是使用CURLfiles={'test.png':base64.b64encode(open('test.png','r').read())}我已经在做这种类型的请求了。谢谢你的回复。我需要把它包括在文件里。它可以工作,但是上传后无法打开/读取图像。但是与curl一起工作这是可行的,因为我将files={'file.png':etc..}改为data=etc。。相反谢谢…我正要切换到urrlib2,我有一个工作的实现。这是有效的,因为我改为使用files={'file.png':etc..}而不是data=etc。。相反谢谢……我正要切换到urrlib2,我有一个工作实现。