Uploading a file via pyCurl

2024/9/19 9:47:39

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 "[email protected]"
-F "[email protected]"
"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?

Answer
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://en.xdnf.cn/q/72760.html

Related Q&A

Speed up computation for Distance Transform on Image in Python

I would like to find the find the distance transform of a binary image in the fastest way possible without using the scipy package distance_trnsform_edt(). The image is 256 by 256. The reason I dont wa…

Prevent Kivy leaving debug messages

I have a simple a Kivy interface that also uses the terminal. Example code:import kivykivy.require(1.0.6)from kivy.app import App from kivy.uix.label import Labelclass MyApp(App):def build(self):return…

django.db.utils.InternalError: (1050, Table django_content_type already exists)

django.db.utils.InternalError: (1050, "Table django_content_type already exists")I just copied a project from my friend, when I run makemirations it runs properly. But for - python3 manage.py…

Use SQLites backup API from Python/SQLAlchemy

Im using an SQLite database from python (with SQLAlchemy). For performance reasons, Id like to populate an in-memory database in the application, and then back up that database to disk.SQLite has a bac…

ABC for String?

I recently discovered abstract base classes (ABCs) in collections and like their clear, systematic approach and Mixins. Now I also want to create customs strings (*), but I couldnt find an ABC for stri…

Get the most relevant word (spell check) from enchant suggest() in Python

I want to get the most relevant word from enchant suggest(). Is there any better way to do that. I feel my function is not efficient when it comes to checking large set of words in the range of 100k or…

How do I get python-markdown to additionally urlify links when formatting plain text?

Markdown is a great tool for formatting plain text into pretty html, but it doesnt turn plain-text links into URLs automatically. Like this one:http://www.google.com/How do I get markdown to add tags …

Best way to read aws credentials file

In my python code I need to extract AWS credentials AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID which are stored in the plain text file as described here: https://docs.aws.amazon.com/sdkref/latest/guid…

Profiling on live Django server?

Ive never done code coverage in Python, but Im looking for something like GCCs gcov, which tells me how many times each line executes, or Apples Shark which gives a hierarchial breakdown of how long ea…

Inset axes anchored to specific points in data coordinates?

Id like to be able to overlay multiple inset axes on top of a set of parent axes, something like this:Ideally, Id like the anchor point of each set of inset axes to be fixed in data coordinates, but fo…