我正在尝试将以下curl代码转换为pycurl。我不想使用请求。我需要使用pycurl,因为请求在我的旧python版本中没有完全正常工作。
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"
有人可以告诉我如何在PyCurl中写出来吗?
答案 0 :(得分:10)
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()