Uploading a file via pyCurl

☆樱花仙子☆ 提交于 2019-12-13 12:29:46

问题


I am trying to convert the following curl code into pycurl. I do not want to use requests. I need to use pycurl because requests is not fully working in my old python version.

curl
-X POST
-H "Accept-Language: en"
-F "images_file=@fruitbowl.jpg"
-F "parameters=@myparams.json"
"https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key={api-key}&version=2016-05-20"

Can someone please show me how to write it out in PyCurl?


回答1:


import pycurl
c = pycurl.Curl()
c.setopt(c.URL, 'https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key={api-key}&version=2016-05-20')
c.setopt(c.POST, 1)
c.setopt(c.HTTPPOST, [("images_file", (c.FORM_FILE, "fruitbowl.jpg"))])
c.setopt(c.HTTPPOST, [("parameters", (c.FORM_FILE, "myparams.json"))])
c.setopt(pycurl.HTTPHEADER, ['Accept-Language: en'])
c.perform()
c.close()


来源:https://stackoverflow.com/questions/38393830/uploading-a-file-via-pycurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!